强制Maven将依赖项复制到target/lib中

Mic*_*ael 241 java dependencies maven-2 maven-3 maven

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

因为它是现在,后mvn clean installtarget文件夹仅包含我的项目的罐子,但没有运行时依赖的.

Geo*_*uba 254

这对我有用:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 如果您希望这种情况一直发生,请删除<profiles> ... <profile>包装器并使<build>标签位于<project>下面 (11认同)
  • 这很好,但它也在复制测试依赖项.我自己添加了`excludeScope`选项(http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html). (4认同)
  • @Georgy这不会在lib /中使用jar,而是包含已编译项目中的类 (3认同)
  • 如果阶段是`package`而不是`install`,也许会更合适 (3认同)
  • 注意:`<excludeScope> test </ excludeScope>`进入`configuration`节点. (2认同)

Joh*_*fer 83

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

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


小智 72

mvn install dependency:copy-dependencies 
Run Code Online (Sandbox Code Playgroud)

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

  • 不知道这怎么不是被接受的答案?无论如何,如果您想将依赖项发送到特定目录,例如名为 'lib' ,可以使用 `mvn dependency:copy-dependencies -DoutputDirectory=lib/` (9认同)

Tra*_*ell 34

看一下Maven依赖插件,特别是依赖:copy-dependencies目标.看一下标题The dependency:copy-dependencies mojo的示例.将outputDirectory配置属性设置为$ {basedir}/target/lib(我相信,你必须测试).

希望这可以帮助.

  • 或者,您可以使用$ {project.build.directory}/lib而不是$ {basedir}/target/lib (14认同)

ruh*_*kus 30

一个简单而优雅的解决方案,用于需要将依赖项复制到目标目录而不使用任何其他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>
Run Code Online (Sandbox Code Playgroud)

然后跑 mvn process-sources

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


小智 24

尝试这样的事情:

<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>
Run Code Online (Sandbox Code Playgroud)


Dun*_*nes 23

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

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

如果省略最后一个参数,则放置依赖关系target/dependencies.

  • 谢谢!这是将项目所需的库复制到某个文件夹中的最简单方法,这样您就可以根据需要将它们复制到其他位置,例如非基于 Maven 的项目。请注意,当然,如果您愿意,您可以只传递硬编码的文件夹来使用,例如 `mvn dependency:copy-dependencies -DoutputDirectory=./lib ` (3认同)

isa*_*pir 14

您所需要的只是pom.xml中的以下代码段build/plugins:

<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>
Run Code Online (Sandbox Code Playgroud)

package当你跑步时,上面将运行

mvn clean package
Run Code Online (Sandbox Code Playgroud)

并且依赖项将被复制到代码段中指定的outputDirectory,即lib在这种情况下.

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

mvn clean package dependency:copy-dependencies
Run Code Online (Sandbox Code Playgroud)

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

    -DoutputDirectory=${project.build.directory}/lib
Run Code Online (Sandbox Code Playgroud)


小智 7

假如

  • 你不想改变pom.xml
  • 你不想要测试作用域(例如junit.jar)或提供的依赖项(例如wlfullclient.jar)

这对我有用:

mvn install dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=target/lib


Ric*_*ler 5

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

以下配置将为Window和Linux生成脚本以启动应用程序(使用生成的路径引用所有依赖项jar,下载所有依赖项(到target/appassembler下面的lib文件夹中).然后可以使用程序集插件来打包整个appassembler目录到一个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> 
Run Code Online (Sandbox Code Playgroud)

用于将direcotry打包为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>
Run Code Online (Sandbox Code Playgroud)


Edu*_*rch 4

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