部署maven项目

Jef*_*rey 6 deployment maven-2

我有一个maven项目,我想用依赖项创建它的分发.我已经尝试了maven-assembly-plugin并使用依赖项构建了jar,但是将所有jar解压缩并将它们重新打包成一个大的单个jar.我喜欢的是像我的jar文件和包含所有依赖项的lib文件夹.然后当我运行它时,我可以运行"java -cp lib/*my.package.MainClass".

用maven做这个的最好方法是什么?还是推荐的部署方式?

谢谢,

杰夫

New*_*ian 8

我在我的项目中使用了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>
Run Code Online (Sandbox Code Playgroud)

然后在您的描述符中,您可以在打包整个事物之前决定布局的方式

<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>
Run Code Online (Sandbox Code Playgroud)

这将创建一个你已经在你的输出目录配置中指定布局的zip文件,打包整个事情作为一个zip文件(可以选择压缩,罐子,战争......),并在我与其他存储库部署.

我跳过点点滴滴使其变得更简单,但我的包扩展到包括批处理文件,dll,配置,doc和JRE所以所需的一切都在同一个zip ...所有需要运行的东西是提取并单击开始.棒球!

我也大概可以使它在以正确的格式与元数据的罐子,只需双击罐子本身开始这一切,我并不需要或有时间来解决这个选项的玩具,但你可能会尝试它.

谨防版本以上Assembly插件的2.1,它会创建重复的项目,如果你的指令,使其能够发现不同地点的同一个文件,这会给你一个lib文件夹使用相同的罐子重复两次.不是非常危险,因为解压缩会使它们崩溃,但仍然很烦人的解压缩问你是否要覆盖文件.此外,你不知道哪个赢了,如果不知何故他们的内容不同.

Maven很棒,但我发现让它工作有时令人沮丧,而且文档有时很难找到和使用.但是,如果使用得当,它将为您节省大量时间.

祝好运