【问题标题】:How to place the output jar into another folder with maven?如何使用 Maven 将输出 jar 放入另一个文件夹?
【发布时间】:2011-10-05 02:15:14
【问题描述】:

我想将我的输出 jar 和 jar-with-dependencies 放到另一个文件夹中(不是在 target/ 中,而是在 ../libs/ 中)。

我该怎么做?

【问题讨论】:

  • 一个小 Maven 提示 - 不要与它的偏好作斗争,它会让你发疯 :)。但是你总是可以使用像 ant 插件这样的东西来做你想做的一切,比如在构建结束时从目标复制到 ../libs...

标签: maven jar


【解决方案1】:

您可以为此目的使用 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 方式。

【讨论】:

  • 也许复制 jar 比移动它们更好?
  • 本书强烈建议使用 maven-dependency-plugin (maven.apache.org/plugins/maven-dependency-plugin) 来解决模块间依赖关系(如果您需要转移到不同的模块)。
  • 您永远不应该使用 maven-dependency-plugin 将其用作模块间依赖项的依赖项。通常,您可以简单地定义一个可行的依赖项...如果在您的构建中存在其他问题...
  • @Torsten 它将 jar 放入 outputDirectory 并且 proguard 无法找到要混淆的 jar。如何使用 proguard-maven-plugin 进行这项工作?
  • @gaurav 我建议您在单独的帖子中提出这个问题,因为这与原始问题无关。这样也更容易为其他人找到。我个人没有使用 proguard 插件的经验。
【解决方案2】:

如果您想将工件复制到项目外部的目录中,解决方案可能是:

  • 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中的路径必须是相对路径是不对的。
  • @ACV 你是对的! outputDirectory 只是一个文件,可以在 source 中看到。我编辑了我的答案。
  • 这个地方的罐子可以放到远程服务器吗?
【解决方案3】:

另一种方法是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>

【讨论】:

  • 这不会在第一次构建时复制,只会在后续构建中复制
  • @theonlygusti 我试过了,效果很好。也许你有一些 eclipse-maven 生命周期映射问题?
  • @theonlygusti 很确定这是因为它运行的阶段是validate。您可以将其更改为 packageinstall 以使其在第一次构建时也能正常工作。
  • &lt;phase&gt;validate&lt;/phase&gt; 更新为&lt;phase&gt;install&lt;/phase&gt;&lt;phase&gt;package&lt;/phase&gt;
  • 我认为 &lt;phase&gt;install&lt;/phase&gt; 为我工作就像我想要的那样。
【解决方案4】:

我会这样做:

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

【讨论】:

    【解决方案5】:

    这种技术对我很有效:

    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>
    

    【讨论】:

    • 第二个 maven-assembly-plugin 技术我发现这是最简单和最主流的解决方案,因为 assembly-plugin 需要一个额外的描述符文件。
    • 在这种情况下,从 pom.xml 元素中提供上下文使我更容易包含在我自己的 pom.xml
    【解决方案6】:

    我特别喜欢使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 2021-04-05
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      相关资源
      最近更新 更多