创建包含属性和依赖项的mvn项目 [英] Create mvn project including properties and dependencies

查看:103
本文介绍了创建包含属性和依赖项的mvn项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建具有一些属性和依赖项的Maven项目?

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

我怎么添加

  <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>

?

我知道我可以从上面使用该命令,并使用一些字符串操作命令.但是,我想知道是否有任何Maven方法可以直接创建具有属性和依赖项设置的pom.xml或稍后添加它们,例如像这样:

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

推荐答案

Don't think maven has this feature OOTB.

您还可以使用通用XML实用工具,例如 XMLStarlet .

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

添加属性:

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

添加依赖项:

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

如果您对我使用的x名称空间有疑问,请阅读以下内容: http://xmlstar .sourceforge.net/doc/UG/ch05s01.html .

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

根据情况,像@Tome所说的那样创建自己的原型可能是一个更好的选择.

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

这篇关于创建包含属性和依赖项的mvn项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆