2009-07-02 261 views
4

我有一個maven項目,我想創建它的依賴關係的分佈。我試過了maven-assembly-plugin,並用依賴關係構建了jar,但是解壓所有jar並將它們重新打包成一個大的單個jar。我想要的就像我的jar文件和一個具有所有依賴關係的lib文件夾。然後當我運行它時,我可以運行「java -cp lib/* my.package.MainClass」。部署maven項目

用maven做這件事最好的方法是什麼?或推薦的部署方式?

感謝,

傑夫

回答

7

我已經使用Maven的組裝只是在我的項目。

首先啓用插件,在POM和打電話給你的裝配配置:

 <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <!--I recommend 2.1 as later versions have a bug that may 
           Duplicate files in your archive 
          --> 
          <version>2.1</version> 
      <!--Executes the packaging along with the mvn package phase 
          --> 
      <executions> 
       <execution> 
        <id>make-assembly</id> 
        <phase>package</phase> 
        <goals> 
         <goal>attached</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <descriptors> 
        <!--Relative path to your descriptor --> 
        <descriptor>src/main/assembly/package.xml 
         </descriptor> 
       </descriptors> 
      </configuration> 
     </plugin> 

然後在你的描述,你可以決定要如何打包整個事情之前,你的佈局是

<assembly> 
    <!-- this will create an extra resource project-1.1.1-package.zip, you can 
     choose jar as well in the format--> 
    <id>package</id> 
    <formats> 
     <format>zip</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <!-- Insert here extra files as configs or, batch files, resources, docs etc--> 
    <fileSets> 
     <fileSet> 
      <directory>src/main/assembly/files</directory> 
      <outputDirectory>/</outputDirectory> 
      <includes> 
       <include>**/conf/*.*</include> 
       <include>**/doc/*.*</include> 
      </includes> 
     </fileSet> 
      <!-- I like to integrate the jre as well... simplifies my deployement --> 
     <fileSet> 
      <directory>target/jre</directory> 
      <outputDirectory>/jre</outputDirectory> 
     </fileSet> 
    </fileSets> 
    <!-- This will scrub your dependencies and add them to your lib folder, I excluded 
     Test stuff as it is not needed, could have declared the resource as a test 
     only phase as well would not have had to exclude it here 
    --> 
    <dependencySets> 
     <dependencySet> 
      <outputDirectory>lib</outputDirectory>   
      <excludes> 
       <exclude>junit:junit</exclude> 
      </excludes> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

這將創建一個zip文件,其中包含您在輸出目錄config中指定的佈局,將整個東西打包爲一個zip文件(您可以選擇zip,jar,war ...)並將其部署到我的存儲庫中。

我跳過了一些小部分,使它更簡單,但我的包擴展到包括批處理文件,DLL,配置,文檔和JRE,所以需要的所有內容都在同一個zip中...所有需要運行的東西都是提取並點擊start.bat!

我也可以將它放入正確使用METADATA格式的jar中,只需雙擊jar本身即可啓動它,我不需要或有時間圍繞此選項進行玩具,但您也可以嘗試。

要小心程序集插件2.1以上的版本,如果你的指令使它能夠在不同的位置找到同一個文件,它會創建重複的條目,這會給你一個lib文件夾,其中兩個jar文件重複。不是很危險,因爲解壓縮會使它們崩潰,但仍然煩人的解壓縮問你是否要覆蓋文件。加上事實上,你不知道哪個贏了,如果他們在內容上有不同的表現。

Maven很棒,但我發現它有時令人沮喪,加上文檔有時很難找到和使用。但是,正確使用它可以爲您節省大量時間。

祝你好運

1

參見:

http://maven.apache.org/shared/maven-archiver/index.html

您應該能夠使用maven-JAR插件來打包存檔,指定主類一起執行與類路徑。它可以爲您的項目生成一個清單文件。

http://maven.apache.org/shared/maven-archiver/examples/classpath.html#Prefix

+0

喬恩,謝謝。我能夠正確生成清單,但是有沒有辦法實際創建我需要的目錄結構?例如,我創建了classpathPrefix lib /,並且希望生成一個lib目錄,並將依賴項複製到該目錄中。 – 2009-07-02 02:46:30

+0

這裏的第二個鏈接告訴你如何做到這一點...http://maven.apache.org/shared/maven-archiver/examples/classpath.html#Prefix – Jon 2009-07-02 03:42:35