Java Maven:如何从命令行更改目标目录的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3908013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven: How to change path to target directory from command line?
提问by Igor Mukhin
Maven: How to change path to target directory from command line?
Maven:如何从命令行更改目标目录的路径?
(I want to use another target directory in some cases)
(在某些情况下我想使用另一个目标目录)
采纳答案by Colin Hebert
You should use profiles.
您应该使用配置文件。
<profiles>
<profile>
<id>otherOutputDir</id>
<build>
<directory>yourDirectory</directory>
</build>
</profile>
</profiles>
And start maven with your profile
并使用您的个人资料启动 Maven
mvn compile -PotherOutputDir
If you really want to define your directory from the command line you could do something like this (NOT recommended at all) :
如果你真的想从命令行定义你的目录,你可以做这样的事情(根本不推荐):
<properties>
<buildDirectory>${project.basedir}/target</buildDirectory>
</properties>
<build>
<directory>${buildDirectory}</directory>
</build>
And compile like this :
并像这样编译:
mvn compile -DbuildDirectory=test
That's because you can't change the target directory by using -Dproject.build.directory
那是因为你不能通过使用改变目标目录 -Dproject.build.directory
回答by Kricket
Colin is correct that a profile should be used. However, his answer hard-codes the target directory in the profile. An alternate solution would be to add a profile like this:
Colin 是正确的,应该使用配置文件。但是,他的回答对配置文件中的目标目录进行了硬编码。另一种解决方案是添加这样的配置文件:
<profile>
<id>alternateBuildDir</id>
<activation>
<property>
<name>alt.build.dir</name>
</property>
</activation>
<build>
<directory>${alt.build.dir}</directory>
</build>
</profile>
Doing so would have the effect of changing the build directory to whatever is given by the alt.build.dir property, which can be given in a POM, in the user's settings, or on the command line. If the property is not present, the compilation will happen in the normal target directory.
这样做的效果是将构建目录更改为由 alt.build.dir 属性给出的任何内容,该属性可以在 POM、用户设置或命令行中给出。如果该属性不存在,则编译将在普通目标目录中进行。
回答by Evgenii
My solution:
我的解决方案:
in pom.xml:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.2</version> <configuration> <outputDirectory>${dir}</outputDirectory> </configuration> </plugin>
command in bash:
mvn package -Ddir="/home/myuser/"
在 pom.xml 中:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.2</version> <configuration> <outputDirectory>${dir}</outputDirectory> </configuration> </plugin>
bash 中的命令:
mvn package -Ddir="/home/myuser/"