如何使用Maven创建具有完整依赖关系的独立应用程序?

Pau*_*lor 9 java maven maven-assembly-plugin maven-jar-plugin

我有一个使用Maven 2构建的桌面Java应用程序(但是如果有帮助的话我可以升级到Maven 3)有许多开源依赖项.我现在正尝试将其打包为独立版,以便最终用户无需安装maven或其他任何东西.

我已经成功地使用maven-assembly-plugin来构建一个包含所有依赖项的jar,但这并不是我想要的,因为在使用LGPL库时,您需要将您正在使用的库重新分发为单独的jar.

我希望Maven使用我的代码构建一个包含jar的zip,并且MANIFEST.MF引用我需要的其他jar和其他jar.这似乎是标准做法,但我看不出怎么做.

这是我的pom的摘录

     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <compilerVersion>1.6</compilerVersion>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.company.widget.Main</mainClass>
                            <packageName>com.company.widget</packageName>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.company.widget.Main</mainClass>
                            <packageName>com.company.widget</packageName>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build> 
Run Code Online (Sandbox Code Playgroud)

编辑:采取卡尔斯的想法

创建了一个名为descriptor.xml的文件

<?xml version="1.0" encoding="UTF-8"?>
<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

和pom包含:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
        </descriptors>
        <archive>
            <manifest>
                <mainClass>com.company.widget.cmdline.Main</mainClass>
                <packageName>com.company.widget</packageName>
                <addClasspath>true</addClasspath>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

现在维护jar并将它们全部放在lib文件夹中,包括我自己的代码

pal*_*lto 5

你应该看看Maven Appassembler插件.使用它可以获得比使用自己的程序集更强大的包.

它为Unix和Windows生成有用的启动脚本,允许您设置预定义的JAVA VM选项,命令行参数和类路径.

它还有一个配置目录的概念,您可以在其中复制用户以后可以更改的默认配置.您还可以将配置目录设置为在类路径上可用.

依赖关系可以保存在Maven样式的"repo"中,也可以使用平面样式的"lib"目录.

您仍然需要用于创建zip或tar存档的程序集插件.

这是一个示例appassembler配置:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.2.2</version>
    <configuration>
        <programs>
            <program>
                <mainClass>com.mytools.ReportTool</mainClass>
                <name>ReportTool</name>
            </program>
        </programs>
        <assembleDirectory>${project.build.directory}/ReportTool</assembleDirectory>
        <repositoryName>lib</repositoryName>
        <repositoryLayout>flat</repositoryLayout>
    </configuration>
    <executions>
        <execution>
            <id>assembly</id>
            <phase>package</phase>
            <goals>
                <goal>assemble</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

要获得zip存档,我使用这个程序集:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/ReportTool</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>/**</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>
Run Code Online (Sandbox Code Playgroud)


Kal*_*dre 3

尝试创建自定义程序集描述符并添加 dependencySet 并确保指定 unpack 为 false。

使用它作为程序集描述符,

<?xml version="1.0" encoding="UTF-8"?>
<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
                        <useProjectArtifact>false</useProjectArtifact>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

将此文件存储为 src/main/ assembly/ assembly.xml 并像这样更新 pom.xml 中的程序集插件配置。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                    <configuration>
                           <descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,这里是程序集描述符参考

http://maven.apache.org/plugins/maven- assembly-plugin/ assembly.html