2008-09-18 70 views
205

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

因为它现在在mvn clean install之后target文件夹只包含我的项目的jar,但没有运行时的依赖关系。

+0

为什么你需要这个?你的maven项目的类型是什么?罐子? – 2008-09-18 22:34:28

+0

我的maven项目的类型是JAR。我需要这个,因为有很多依赖关系,我试图将jar部署为可执行文件。 – Michael 2008-09-18 22:56:07

+3

你在使用程序集吗? – 2009-04-19 07:35:29

回答

2

如果你让你的项目为战争或耳朵类型的maven将复制依赖关系。

2

您可以使用Shade Plugin创建一个超级jar包,您可以在其中捆绑所有第三方依赖项。

32

看看Maven dependency plugin,特别是dependency:copy-dependencies goal。请看the example,标题依赖关系:复制依赖项mojo。将outputDirectory配置属性设置为$ {basedir}/target/lib(我相信,您必须测试)。

希望这会有所帮助。

+13

或者,您可以使用$ {project.build.directory}/lib而不是$ {basedir}/target/lib – Cuga 2010-07-06 14:42:48

73

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

  • 如果您希望捆绑你的依赖成WAR或EAR文件,然后简单地设置您的项目以EAR或WAR的包装类型。 Maven会将依赖关系捆绑到正确的位置。
  • 如果要创建包含代码以及所有依赖项的JAR文件,请使用assembly插件与与jar相关的描述符。 Maven将生成一个完整的JAR文件,其中包含所有类以及来自任何依赖关系的类。
  • 如果你想简单地拉你的依赖复制到目标目录交互,然后使用dependency插件来将文件复制英寸
  • 如果你想在依赖拉一些其他类型的处理,那么你可能会需要生成自己的插件。有API可以获取依赖关系列表以及它们在磁盘上的位置。你将不得不从那里...
223

这个工作对我来说:

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

如果您希望始终发生此情况,请删除 ... 包装并制作标记只是在 2012-11-06 15:34:12

+2

@Georgy这不会对lib /中的罐子抱怨,但包括已编译项目中的类 – Midhat 2012-11-26 09:04:32

+4

这很好,但它也在复制测试依赖关系。我自己添加`excludeScope`选项(http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html)。 – 2013-08-09 08:44:37

4

如果你想与所有提供您的应用程序JAR的包,一起它的依赖和一些脚本调用MainClass,看看appassembler-maven-plugin

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

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

的组件描述符(在SRC /主/组件)以封装direcotry以zip将:

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

只是为了说明已经简要说过的内容。我想创建一个可执行的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编译组件:组件产生所需的我,以项目项目目标目录中的0.1-SNAPSHOT-jar-with-dependencies.jar。

(3)我跑了与Java的JAR的罐子我的项目-0.1-快照JAR-与-dependencies.jar

28

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

1

发现它是嵌入重依赖沉重的解决方案,但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> 
21

尝试是这样的:

<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> 
53
mvn install dependency:copy-dependencies 

对我的作品具有依赖性目标文件夹中创建的目录。喜欢它!

19

如果你想这样做,一个偶然的基础上(因此不想改变你的POM),请尝试以下命令行:

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

如果省略了last argument的依赖关系被置于在target/dependencies

4

假设

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

这里北京时间什么工作对我来说:

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

所有你需要的是里面的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> 

以上将在package阶段运行当您运行时

mvn clean package 

依赖关系将被复制到片段中指定的outputDirectory中,即在这种情况下为lib

如果您只是偶尔需要这样做,那么不需要对pom.xml进行任何更改。只需运行以下命令:

mvn clean package dependency:copy-dependencies 

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

-DoutputDirectory=${project.build.directory}/lib 
0
<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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.edge</groupId> 
    <artifactId>STORM2</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>STORM2</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <profiles> 

    </profiles> 
<build> 
    <plugins> 
      <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <archive> 
       <manifest> 
       <mainClass>com.edge.STORM2.LogAnalyserStorm</mainClass> 
       </manifest> 
       </archive> 
       <descriptorRefs> 
       <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
      </configuration> 
      </plugin> 

    </plugins> 
    </build> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
    <groupId>org.apache.storm</groupId> 
    <artifactId>storm-core</artifactId> 
    <version>1.1.1</version> 
    <scope>provided</scope> 
</dependency> 
    </dependencies> 
</project> 

@john 这是我的POM,我错过什么?创建的jar与依赖关系,不包含任何lib类或罐子

相关问题