Create mvn project including properties and dependencies

自古美人都是妖i 提交于 2019-12-25 01:49:31

问题


How can I create a Maven project with some properties and dependencies set?

E.g. when creating a project via:

mvn archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId=com.mycompany.app \
  -DartifactId=my-app \
  -DinteractiveMode=false

how can I add

  <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  ...
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20090211</version>
    </dependency>

?

I know that I could use the command from above a use some string manipulation commands. However, I want to know if there is any Maven way to either directly create a pom.xml with properties and dependencies set or to add them later on, e.g. something like:

mvn <add-properties> -Dmaven.compiler.source=1.7 -Dmaven.compiler.target=1.7
mvn <add-dependency> -DgroupId=org.json -DartifactId=json -Dversion=20090211

回答1:


Don't think maven has this feature OOTB.

You can also use a generic XML utility tool e.g. XMLStarlet.

Adding properties:

xmlstarlet ed -N x=http://maven.apache.org/POM/4.0.0 \
-s /x:project -t elem -n properties -v "" \
-s /x:project/properties -t elem -n maven.compiler.source -v 1.7 \
-s /x:project/properties -t elem -n maven.compiler.target -v 1.7 \
pom.xml

Adding a dependency:

xmlstarlet ed -N x=http://maven.apache.org/POM/4.0.0 \
-s /x:project/x:dependencies -t elem -n dependency -v "" \
-s "/x:project/x:dependencies/dependency[last()]" -t elem -n groupId -v org.json \
-s "/x:project/x:dependencies/dependency[last()]" -t elem -n artifactId -v json \
-s "/x:project/x:dependencies/dependency[last()]" -t elem -n version -v 2009211 \
pom.xml

Read this if you have questions regarding the x namespace I used: http://xmlstar.sourceforge.net/doc/UG/ch05s01.html.

Depending on the scenario, creating your own archetype like what @Tome had said might be a better option.



来源:https://stackoverflow.com/questions/17615428/create-mvn-project-including-properties-and-dependencies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!