2009-07-02 264 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