我如何让我的项目的运行时依赖复制到目标/lib文件夹?

就像现在一样,在mvn清洁安装后,目标文件夹只包含我的项目的jar,但没有运行时依赖项。


当前回答

这对我来说很管用:

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

其他回答

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

下面的配置将为windows和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> 

将目录目录打包为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>

你所需要的只是在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>

当您运行时,上面的代码将在包阶段运行

mvn clean package

依赖项将被复制到代码片段中指定的outputDirectory,在本例中为lib。

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

mvn clean package dependency:copy-dependencies

重写默认位置${project.build。目录}/依赖,添加一个名为outputDirectory的系统属性,即

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

假设

您不希望更改pom.xml 你不想要测试范围(例如junit.jar)或提供的依赖关系(例如wlfullclient.jar)

以下是对我有效的方法:

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

工作为我在目标文件夹中创建的依赖目录。喜欢它!

这对我来说很管用:

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