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

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


当前回答

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

If you want to bundle your dependencies into a WAR or EAR file, then simply set the packaging type of your project to EAR or WAR. Maven will bundle the dependencies into the right location. If you want to create a JAR file that includes your code along with all your dependencies, then use the assembly plugin with the jar-with-dependencies descriptor. Maven will generate a complete JAR file with all your classes plus the classes from any dependencies. If you want to simply pull your dependencies into the target directory interactively, then use the dependency plugin to copy your files in. If you want to pull in the dependencies for some other type of processing, then you will probably need to generate your own plugin. There are APIs to get the list of dependencies, and their location on disk. You will have to take it from there...

其他回答

简单地把刚才说过的内容讲一遍。我想创建一个可执行JAR文件,其中包括我的依赖关系和我的代码。这招对我很管用:

(1)在pom中,在<build><plugins>下,我包括:

<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

你可以在你的项目目录中放置一个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文档中找到。 请注意,除非输入绝对路径,否则路径是相对于实际设置文件所在的目录进行解析的。

当你执行maven命令时,你可以像下面这样使用设置文件:

mvn -s settings.xml clean install

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

如果您想交付应用程序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>

这对我来说很管用:

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

这是一种用于嵌入严重依赖关系的重载解决方案,但Maven的Assembly Plugin对我来说是有用的。

@Rich Seller的回答应该是有效的,尽管对于更简单的情况,你应该只需要从使用指南中摘录以下内容:

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