Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 873
matts Avatar asked Nov 12 '22 19:11

matts


1 Answers

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
like image 86
Pete Avatar answered Nov 17 '22 00:11

Pete