2008-09-18 67 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類或罐子

相關問題