【发布时间】:2011-10-05 02:15:14
【问题描述】:
我想将我的输出 jar 和 jar-with-dependencies 放到另一个文件夹中(不是在 target/ 中,而是在 ../libs/ 中)。
我该怎么做?
【问题讨论】:
-
一个小 Maven 提示 - 不要与它的偏好作斗争,它会让你发疯 :)。但是你总是可以使用像 ant 插件这样的东西来做你想做的一切,比如在构建结束时从目标复制到 ../libs...
我想将我的输出 jar 和 jar-with-dependencies 放到另一个文件夹中(不是在 target/ 中,而是在 ../libs/ 中)。
我该怎么做?
【问题讨论】:
您可以为此目的使用 maven-jar-plugin 的 outputDirectory 参数:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>../libs</outputDirectory>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
但正如 cdegroot 所写,您最好不要使用 maven 方式。
【讨论】:
如果您想将工件复制到项目外部的目录中,解决方案可能是:
maven-jar-plugin 并配置outputDirectory
maven-antrun-plugin 和复制任务copy-maven-plugin 叶夫根尼·戈尔丁copy-maven-plugin 的示例是:
<plugin>
<groupId>com.github.goldin</groupId>
<artifactId>copy-maven-plugin</artifactId>
<version>0.2.5</version>
<executions>
<execution>
<id>deploy-to-local-directory</id>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<skipIdentical>false</skipIdentical>
<failIfNotFound>false</failIfNotFound>
<resources>
<resource>
<description>Copy artifact to another directory</description>
<targetPath>/your/local/path</targetPath>
<directory>${project.build.directory}</directory>
<includes>
<include>*.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
【讨论】:
java.lang.ClassNotFoundException: org.sonatype.aether.RepositorySystem
outputDirectory 只是一个文件,可以在 source 中看到。我编辑了我的答案。
另一种方法是maven-resources-plugin(在此处查找当前版本):
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-files-on-build</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/[TO-DIR]</outputDirectory>
<resources>
<resource>
<directory>[FROM-DIR]</directory>
<!--<include>*.[MIME-TYPE]</include>-->
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
【讨论】:
validate。您可以将其更改为 package 或 install 以使其在第一次构建时也能正常工作。
<phase>validate</phase> 更新为<phase>install</phase> 或<phase>package</phase>
<phase>install</phase> 为我工作就像我想要的那样。
我会这样做:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="target/${project.artifactId}-exec.jar" tofile="../../docker/${project.artifactId}.jar"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
【讨论】:
这种技术对我很有效:
http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
【讨论】:
pom.xml 的 pom.xml 中我特别喜欢使用maven-resources-plugin(见here)的解决方案,因为它已经包含在maven中,所以不需要额外下载,而且在项目的特定阶段进行复制也是非常可配置的(见here 了解和了解阶段)。这种方法最好的部分是它不会弄乱以前的任何流程或构建您之前的构建:)
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/dir/where/you/want/to/put/jar</outputDirectory>
<resources>
<resource>
<directory>/dir/where/you/have/the/jar</directory>
<filtering>false</filtering>
<includes>
<include>file-you-want-to.jar</include>
<include>another-file-you-want-to.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
当然,您也可以在整个 XML 中使用像 ${baseDir} 这样的插值变量和其他类似的好东西。你可以使用通配符来解释here
【讨论】: