Reputation: 6897
Is there a command or Maven plugin I can use to add a dependency to a POM from the command line?
For example, I want to type something like:
mvn lazy:add-dependency -DgroupId=com.mycompany -DartifactId=derp -Dversion=1.0
and have it modify the dependencies section of the POM in the current directory:
<dependencies>
... other dependencies ...
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>derp</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
An external command to add the above XML would also work, but I'd prefer one that doesn't require me to write an XSL stylesheet.
Upvotes: 7
Views: 4092
Reputation: 21
Not sure if you ever solved this, but I have done something similar in the past with xsltproc ( I Know you said not to use one, but I never found another way of doing it).
function merge_xml ()
{
TEMP_XML1="some-temp-file1.xml"
TEMP_XML2="some-temp-file2.xml"
cat > $TEMP_XML1
cp $1 $TEMP_XML2
echo "Merging XML stream from $1 into $2" >&2
xsltproc --stringparam with "$TEMP_XML1" merge.xslt "$TEMP_XML2" | tidy -xml -indent -quiet -wrap 500 -output $2
}
merge.xslt can be found here http://www2.informatik.hu-berlin.de/~obecker/XSLT/merge/merge.xslt
Then to call the Bash function:
merge_xml $PROJECT_ROOT/content/pom.xml $PROJECT_ROOT/content/pom.xml << EOF
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>jar</classifier>
</dependency>
</dependencies>
</project>
EOF
Upvotes: 1
Reputation: 2309
Following Andrew's comment:
Example using sed:
sed 's/<dependencies>/<dependencies>\r\n<!--ghost-->\r\n<dependency>\r\n<groupId>org.ghost4j<\/groupId>\r\n<artifactId>ghost4j<\/artifactId>\r\n<version>0.5.0<\/version>\r\n<\/dependency>\r\n<!--ghost-->/g' pom.xml > pom2.xml
Replaces the dependencies tag with the dependencies tag followed by the new dependency (inserts the new dependency first in the list.
Creates a new file pom2.xml with the new dependency (this can be changed to overwrite the original file using: pom.xml > pom.xml
Upvotes: 1
Reputation: 10311
I'm not aware of an existing Plugin that does this, but it could be fairly straightforward to implement your own Maven plugin leveraging Ant and XMLTask.
Upvotes: 0