如何仅为包装jar指定outputDirectory?
http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html显示所有参数,但是如何在命令行或pom.xml中设置它们?
在命令行上
-DoutputDirectory=<path>
并在pom.xml中
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>/my/path</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
关于命令行用法:
The parameter documentation指定将参数初始化为属性${project.build.directory}
(这是指向目标文件夹的属性)的值
这是这意味着什么:
对于要直接从CLI执行的mojos,它们的参数通常提供了一种通过系统属性而不是POM中的
<configuration/>
部分进行配置的方法。这些参数的插件文档将列出表示配置的系统属性的表达式。在上面的mojo中,参数url与表达式${query.url}
相关联,这意味着它的值可以由系统属性query.url
指定,如下所示:
mvn myquery:query -Dquery.url=http://maven.apache.org
参考:
然而,${project.build.directory}
不是it's a property的Project对象的系统属性Build
。
您不能直接在命令行上设置maven的内部属性,但是您可以通过在pom.xml中添加占位符来实现一些小技巧:
<build>
<directory>${dir}</directory>
</build>
现在,输出目录通过命令行中的属性设置(使用-Ddir=somedirectory
)。缺点:现在你必须在命令行上使用-Ddir
参数。
但是这里也有帮助。只需在配置目录时使用配置文件:
<profiles>
<profile>
<id>conf</id>
<build>
<directory>${dir}</directory>
</build>
</profile>
</profiles>
现在你可以做到
# everything goes in someOtherDir instead of target
mvn clean install -Pconf -Ddir=someOtherDir
或者说老了
# everything goes in target
mvn clean install
现在,如果您只想从命令行更改jar outputDirectory而不重定向目标中的所有内容,我们将修改配置文件以从命令行属性配置插件:
<profiles>
<profile>
<id>conf</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>${dir}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
用法与上述相同:
# everything goes in someOtherDir instead of target
mvn clean install -Pconf -Ddir=someOtherDir
感谢@Sean Patrick Floyd的出色解释。
我不想使用mvn
开关创建配置文件并使用-P
,而是想使用另一种方法来创建属性$ {dir}的默认值。
只需将${dir}
的默认值定义为${project.build.directory}
<properties>
<dir>${project.build.directory}</dir>
</properties>
和@Sean Patrick Floyd一样,设置outputDirectory。
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>${dir}</outputDirectory>
</configuration>
</plugin>
</plugins>
现在你可以做到
# everything goes in someOtherDir instead of target
mvn clean install -Ddir=someOtherDir
或者说老了
# everything goes in target
mvn clean install
如果您希望将依赖关系jar复制到目标文件夹,请使用maven-dependency-plugin。
<project>
...
...
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>