【问题标题】:Make Maven to copy dependencies into target/lib让 Maven 将依赖项复制到 target/lib
【发布时间】:2010-09-10 23:46:12
【问题描述】:

如何将项目的运行时依赖项复制到target/lib 文件夹中?

就像现在一样,在mvn clean install 之后,target 文件夹只包含我的项目的 jar,但没有运行时依赖项。

【问题讨论】:

  • 为什么需要这个?您的 Maven 项目的类型是什么?罐子?
  • 我的maven项目的类型是JAR。我需要这个,因为有很多依赖项,我正在尝试将 jar 部署为可执行文件。
  • 谨慎使用程序集 - 如果您在各个部门之间有重叠的包/类,您可能会遇到麻烦。

标签: java maven dependencies maven-2 maven-3


【解决方案1】:

您可以在项目目录中放置一个settings.xml 文件,其基本配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>.m2/repository</localRepository>
    <interactiveMode/>
    <offline/>
    <pluginGroups/>
    <servers/>
    <mirrors/>
    <proxies/>
    <profiles/>
    <activeProfiles/>
</settings>

关于这些设置的更多信息可以在官方Maven docs找到。
请注意,除非您输入绝对路径,否则该路径是相对于实际设置文件所在的目录解析的。

执行maven命令时可以使用如下设置文件:

mvn -s settings.xml clean install

旁注:我在我的 GitLab CI/CD 管道中使用它,以便能够为多个作业缓存 maven 存储库,这样就不需要为每个作业执行下载依赖项。 GitLab 只能缓存项目目录中的文件或目录,因此我在项目目录中引用了一个目录。

【讨论】:

    【解决方案2】:

    如果您想交付应用程序 jar 包及其所有依赖项和一些用于调用 MainClass 的脚本,请查看 appassembler-maven-plugin

    以下配置将为 Window 和 Linux 生成脚本以启动应用程序(生成的路径引用所有依赖项 jar,下载所有依赖项(到 target/appassembler 下的 lib 文件夹中)。然后可以使用 assembly plugin将整个 appassembler 目录打包到一个 zip 中,该 zip 与 jar 一起安装/部署到存储库中。

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>appassembler-maven-plugin</artifactId>
        <version>1.0</version>
        <executions>
          <execution>
            <id>generate-jsw-scripts</id>
            <phase>package</phase>
            <goals>
              <goal>generate-daemons</goal>
            </goals>
            <configuration>
              <!--declare the JSW config -->
              <daemons>
                <daemon>
                  <id>myApp</id>
                  <mainClass>name.seller.rich.MyMainClass</mainClass>
                  <commandLineArguments>
                    <commandLineArgument>start</commandLineArgument>
                  </commandLineArguments>
                  <platforms>
                    <platform>jsw</platform>
                  </platforms>              
                </daemon>
              </daemons>
              <target>${project.build.directory}/appassembler</target>
            </configuration>
          </execution>
          <execution>
            <id>assemble-standalone</id>
            <phase>integration-test</phase>
            <goals>
              <goal>assemble</goal>
            </goals>
            <configuration>
              <programs>
                <program>
                  <mainClass>name.seller.rich.MyMainClass</mainClass>
                  <!-- the name of the bat/sh files to be generated -->
                  <name>mymain</name>
                </program>
              </programs>
              <platforms>
                <platform>windows</platform>
                <platform>unix</platform>
              </platforms>
              <repositoryLayout>flat</repositoryLayout>
              <repositoryName>lib</repositoryName>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-4</version>
        <executions>
          <execution>
            <phase>integration-test</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>src/main/assembly/archive.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin> 
    

    将目录打包为 zip 的程序集描述符(在 src/main/assembly 中)是:

    <assembly>
      <id>archive</id>
      <formats>
        <format>zip</format>
      </formats>
      <fileSets>
        <fileSet>
         <directory>${project.build.directory}/appassembler</directory>
         <outputDirectory>/</outputDirectory>
        </fileSet>
      </fileSets>
    </assembly>
    

    【讨论】:

      【解决方案3】:

      您只需要在 pom.xml 的build/plugins 中添加以下 sn-p:

      <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <executions>
              <execution>
                  <phase>prepare-package</phase>
                  <goals>
                      <goal>copy-dependencies</goal>
                  </goals>
                  <configuration>
                      <outputDirectory>${project.build.directory}/lib</outputDirectory>
                  </configuration>
              </execution>
          </executions>
      </plugin>
      

      上面运行时会在package阶段运行

      mvn clean package
      

      并且依赖将被复制到sn-p中指定的outputDirectory,即lib

      如果您只想偶尔这样做,则不需要更改 pom.xml。只需运行以下命令:

      mvn clean package dependency:copy-dependencies
      

      要覆盖默认位置 ${project.build.directory}/dependencies,请添加一个名为 outputDirectory 的系统属性,即

          -DoutputDirectory=${project.build.directory}/lib
      

      【讨论】:

        【解决方案4】:

        假设

        • 你不想改变 pom.xml
        • 您不希望测试范围(例如 junit.jar)或提供的依赖项(例如 wlfullclient.jar)

        这对我有用:

        mvn 安装依赖项:复制依赖项 -DincludeScope=runtime -DoutputDirectory=target/lib

        【讨论】:

          【解决方案5】:

          最好的方法取决于你想做什么:

          • 如果要将依赖项捆绑到 WAR 或 EAR 文件中,只需将项目的打包类型设置为 EAR 或 WAR。 Maven 会将依赖项捆绑到正确的位置。
          • 如果您想创建一个包含您的代码以及所有依赖项的 JAR 文件,请使用带有 jar-with-dependencies 描述符的 assembly 插件。 Maven 将生成一个完整的 JAR 文件,其中包含您的所有类以及来自任何依赖项的类。
          • 如果您只想以交互方式将依赖项拉入目标目录,请使用dependency 插件将文件复制到其中。
          • 如果您想引入依赖项以进行其他类型的处理,那么您可能需要生成自己的插件。有一些 API 可以获取依赖项列表,以及它们在磁盘上的位置。你必须从那里拿走它......

          【讨论】:

            【解决方案6】:

            一种简单而优雅的解决方案,适用于需要将依赖项复制到目标目录而不使用任何其他 maven 阶段的情况(我发现这在使用 Vaadin 时非常有用)。

            完整的 pom 示例:

            <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
            
                <modelVersion>4.0.0</modelVersion>
                <groupId>groupId</groupId>
                <artifactId>artifactId</artifactId>
                <version>1.0</version>
            
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis</groupId>
                        <artifactId>mybatis-spring</artifactId>
                        <version>1.1.1</version>
                    </dependency>
                </dependencies>
            
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-dependency-plugin</artifactId>
                                <executions>
                                    <execution>
                                        <phase>process-sources</phase>
            
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
            
                                        <configuration>
                                            <outputDirectory>${targetdirectory}</outputDirectory>
                                        </configuration>
                                    </execution>
                                </executions>
                        </plugin>
                    </plugins>
                </build>
            </project>
            

            然后运行mvn process-sources

            jar文件依赖可以在/target/dependency找到

            【讨论】:

            • maven-dependency-plugin(目标“copy-dependencies”、“unpack”)不受 m2e 支持。 :-(
            • @Gobliins 使用 ${project.build.directory}/lib 而不是 ${targetdirectory}
            【解决方案7】:

            试试这样的:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>  
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>MainClass</mainClass>
                    </manifest>
                </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            

            【讨论】:

            • @Thomas 我想是maven clean install,然后你会在target中找到lib
            • 只复制 1 个依赖项我需要做什么?
            • lib/ 帮了我很多。谢谢!
            • 会将install 阶段替换为process-resources,以便在build 目标运行之前复制依赖项
            【解决方案8】:

            如果您想偶尔执行此操作(因此不想更改您的 POM),请尝试以下命令行:

            mvn 依赖:copy-dependencies -DoutputDirectory=${project.build.directory}/lib

            如果省略last argument,则依赖项放在target/dependencies

            【讨论】:

            • 谢谢!这是将项目所需的库复制到某个文件夹中的最简单方法,这样您就可以在需要时将它们复制到其他地方,例如一个非基于 Maven 的项目。请注意,当然,如果您愿意,您可以只传递一个硬编码的文件夹来使用,例如mvn dependency:copy-dependencies -DoutputDirectory=./lib
            • 你能用 pom.xml 做吗?
            【解决方案9】:

            这对我有用:

            <project>
              ...
              <profiles>
                <profile>
                  <id>qa</id>
                  <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>
                </profile>
              </profiles>
            </project>
            

            【讨论】:

            • 如果您希望这种情况一直发生,请删除 ... 包装器并使 标记位于 下方
            • @Georgy 这不会欺骗 lib/ 中的 jar,而是包含已编译项目中的类
            • 这很好,但它也在复制测试依赖项。我给自己添加了excludeScope 选项(maven.apache.org/plugins/maven-dependency-plugin/…)。
            • 注意:&lt;excludeScope&gt;test&lt;/excludeScope&gt; 进入 configuration 节点。
            • 如果相位是package而不是install可能会更合适
            【解决方案10】:
            mvn install dependency:copy-dependencies 
            

            适用于我在目标文件夹中创建的依赖项目录。喜欢!

            【讨论】:

            • 感谢您提供这个简单的选项。我改用了 package 目标,但做了我想要的。
            【解决方案11】:

            这是嵌入重度依赖项的重度解决方案,但 Maven 的 Assembly Plugin 为我解决了问题。

            @Rich Seller's answer 应该可以工作,尽管对于更简单的情况,您应该只需要usage guide 的这段摘录:

            <project>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-assembly-plugin</artifactId>
                            <version>2.2.2</version>
                            <configuration>
                                <descriptorRefs>
                                    <descriptorRef>jar-with-dependencies</descriptorRef>
                                </descriptorRefs>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </project>
            

            【讨论】:

            • 您的代码示例并没有解决问题,它只是将所有内容捆绑到一个 JAR 中。是的,汇编插件可以用来实现这个目标,但不是这样的。
            • 虽然,在进一步阅读中,也许你正在回复this comment
            • 时间太长了,我都不记得了……另外,自从我在上一家公司专注于 Linux 管理以来,我已经相当生疏了 — 但感谢您的反馈!
            【解决方案12】:

            如果您在 Eclipse 中的 Tomcat 服务器上运行时遇到依赖项未出现在 WEB-INF/lib 文件中的问题,请查看以下内容:

            ClassNotFoundException DispatcherServlet when launching Tomcat (Maven dependencies not copied to wtpwebapps)

            您只需在 Project Properties > Deployment Assembly 中添加 Maven 依赖项。

            【讨论】:

              【解决方案13】:

              只是为了简要说明已经说过的内容。我想创建一个可执行的 JAR 文件,其中包含我的依赖项以及我的代码。这对我有用:

              (1) 在 pom 中,在 下,我包括:

              <plugin>
                  <artifactId>maven-assembly-plugin</artifactId>
                  <version>2.2-beta-5</version>
                  <configuration>
                      <archive>
                          <manifest>
                              <mainClass>dk.certifikat.oces2.some.package.MyMainClass</mainClass>
                          </manifest>
                      </archive>
                      <descriptorRefs>
                          <descriptorRef>jar-with-dependencies</descriptorRef>
                      </descriptorRefs>
                  </configuration>
              </plugin>
              

              (2) 运行 mvn compile assembly:assembly 在项目的目标目录中生成所需的 my-project-0.1-SNAPSHOT-jar-with-dependencies.jar。

              (3) 我使用 java -jar my-project-0.1-SNAPSHOT-jar-with-dependencies.jar 运行 JAR

              【讨论】:

              • 在 (3) 中找不到主类
              【解决方案14】:

              看看Maven dependency plugin,特别是dependency:copy-dependencies goal。查看 The dependency:copy-dependencies mojo 标题下的 the example。将 outputDirectory 配置属性设置为 ${basedir}/target/lib(我相信,您必须进行测试)。

              希望这会有所帮助。

              【讨论】:

              • 或者,您可以使用 ${project.build.directory}/lib 而不是 ${basedir}/target/lib
              【解决方案15】:

              您可以使用Shade Plugin 创建一个 uber jar,您可以在其中捆绑所有 3rd 方依赖项。

              【讨论】:

                【解决方案16】:

                如果您将项目设为 war 或 ear 类型,maven 将复制依赖项。

                【讨论】:

                  猜你喜欢
                  • 2010-09-10
                  • 2015-02-28
                  • 2012-09-10
                  • 1970-01-01
                  • 2012-04-22
                  • 2013-11-25
                  • 1970-01-01
                  • 2011-03-26
                  • 2011-11-13
                  相关资源
                  最近更新 更多