Is there a way to add a dependency on Maven POM from the command line?

Is there a Maven command or plugin that I can use to add a POM dependency from the command line?

For example, I want to print something like:

mvn lazy:add-dependency -DgroupId=com.mycompany -DartifactId=derp -Dversion=1.0 

and change the POM dependency section 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 will also work, but I would prefer that I don't write me an XSL stylesheet.

+4
source share
3 answers

Not sure if you ever solved this, but I did something similar in the past with xsltproc (I know you said you weren’t using it, but I never found another way to do this).

 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 
+1
source

I don't know about an existing plugin that does this, but it can be quite simple: implement your own Maven plugin using Ant and XMLTask .

0
source

Andrew's next comment:

An example of 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 dependency tag with the dependency tag, followed by the new dependency (inserts the new dependency first into the list.

Creates a new pom2.xml file with a new dependency (this can be changed to overwrite the original file using: pom.xml> pom.xml

0
source

Source: https://habr.com/ru/post/1442123/


All Articles