关于java:force Maven将依赖项复制到target / lib

关于java:force Maven将依赖项复制到target / lib

force Maven to copy dependencies into target/lib

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

现在,在mvn clean install之后,target文件夹仅包含我项目的jar,而没有任何运行时依赖项。


这对我有用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<project>
  ...
  <profiles>
    <profile>
      <id>qa</id>
      <build>
        <plugins>
          <plugin>
            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>

最佳方法取决于您想做什么:

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


1
mvn install dependency:copy-dependencies

对我有用,它在目标文件夹中创建了依赖项目录。喜欢它!


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

希望这可以帮助。


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

完整的pom示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<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>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                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中找到


尝试这样的事情:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<plugin>
<groupId>org.apache.maven.plugins</groupId>
maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
   
        <manifest>  
            true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>MainClass</mainClass>
        </manifest>
    </archive>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    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>


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

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

如果省略最后一个参数,则相关性将放置在target/dependencies中。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<plugin>
    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阶段运行

1
mvn clean package

并将相关性复制到代码段中指定的outputDirectory中,在这种情况下为lib

如果您只想偶尔执行此操作,则无需更改pom.xml。 只需运行以下命令:

1
mvn clean package dependency:copy-dependencies

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

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

假如

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

这对我有用:

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

如果要交付捆绑的应用程序jar,所有依赖关系以及一些调用MainClass的脚本,请查看appassembler-maven-plugin。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    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>
    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中)为:

1
2
3
4
5
6
7
8
9
10
11
  <id>archive</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
     <directory>${project.build.directory}/appassembler</directory>
     <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

如果您使项目成为战争或战争类型,那么maven将复制依赖关系。


这是用于嵌入大量依赖项的繁重解决方案,但是Maven的Assembly Plugin为我解决了这个难题。

@Rich Seller's的答案应该有用,尽管在更简单的情况下,您只需要使用指南中的以下摘录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<project>
    <build>
        <plugins>
            <plugin>
                maven-assembly-plugin</artifactId>
                <version>2.2.2</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<plugin>
    maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <configuration>
       
            <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


您可以使用Shade插件创建一个uber jar,在其中可以捆绑所有第3方依赖性。


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

启动Tomcat时发生ClassNotFoundException DispatcherServlet(Maven依赖关系未复制到wtpwebapps)

您只需要在"项目属性">"部署程序集"中添加Maven依赖项。


推荐阅读

    更新文件命令linux?

    更新文件命令linux?,工作,系统,地址,信息,时间,命令,目录,基础,标准,网络,lin

    linux运行图形界命令?

    linux运行图形界命令?,系统,密码,地址,电脑,图形界面,地方,工具,界面,终端,

    linux怎样运行命令?

    linux怎样运行命令?,系统,工作,信息,基础,地址,命令,目录,工具,密码,一致,Lin

    linux下读取文件命令?

    linux下读取文件命令?,系统,工作,地址,数字,图片,信息,网络,命令,文件,一致,l

    linux改文件夹名命令?

    linux改文件夹名命令?,名字,软件,命令,文件,系统,目录,目标,文件名,源文件,

    linux建文件命令格式?

    linux建文件命令格式?,系统,名字,名称,时间,密码,命令,文件,文件夹,不了,数

    下载文件的命令linux?

    下载文件的命令linux?,平台,工具,服务,密码,软件,网络,位置,代理,手机,工作,

    linux命令文件传输?

    linux命令文件传输?,系统,数据,命令,文件,基本知识,源文件,目录,目标,功能,

    linux编译完运行命令?

    linux编译完运行命令?,系统,代码,环境,工具,信息,命令,文件,程序,终端,编辑,

    linux文件全选命令?

    linux文件全选命令?,电脑,系统,环境,代码,平台,服务,快捷键,文件,命令,权限,l

    linux浏览文件类命令?

    linux浏览文件类命令?,系统,信息,数据,情况,命令,标准,时间,文件,概念,管理,

    linux打包文件夹命令?

    linux打包文件夹命令?,系统,工具,管理,图片,文件,命令,位置,软件,目录,格式,l

    linux中统计文件命令?

    linux中统计文件命令?,系统,信息,数据,情况,工作,文件,时间,档案,标准,名称,L

    linux复制几行命令行?

    linux复制几行命令行?,系统,位置,基本知识,设备,软件,文件,命令,源文件,连

    linux命令程序运行?

    linux命令程序运行?,状态,系统,服务,情况,命令,进程,软件,数据,发行,时间,Lin

    linux重名名文件命令?

    linux重名名文件命令?,图片,名称,名字,文件,命令,位置,代码,软件,系统,文件

    linux的建立文件命令?

    linux的建立文件命令?,名称,系统,时间,名字,命令,文件夹,位置,密码,不了,文

    linux06文件命令?

    linux06文件命令?,数字,系统,工作,第一,名字,信息,管理,文件,目录,命令,linux

    linux将文件备份命令?

    linux将文件备份命令?,系统,密码,设备,软件,通讯,较大,认证,服务,数据,文件,L