如何使用Maven创建具有完整依赖关系的独立应用程序? [英] How do you create a standalone application with dependencies intact using Maven?

查看:116
本文介绍了如何使用Maven创建具有完整依赖关系的独立应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a desktop Java application built using Maven 2 (but I could upgrade to Maven 3 if that helps) with a number of open source dependencies. I'm now trying to package it up as a standalone to make it available to end users without them needing to install maven or anything else.

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

I've successfully used maven-assembly-plugin to build a single jar containing all dependencies but this is not really what I want because when using LGPL libraries you are meant to redistribute the libraries you are using as separate jars.

我希望Maven构建一个包含我的代码的jar和一个 MANIFEST.MF 的zip,它指的是我需要的其他罐子和其他罐子一起。这似乎是标准做法,但我看不出怎么做。

I want Maven to build a zip containing a jar with my code and a MANIFEST.MF that refers to the other jars I need together with the other jars. This seems like standard practice but I cannot see how to do it.

这是我的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> 

编辑:采取加尔斯的想法

Taken on Kals idea

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

created a file called 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>

和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>

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

Now maintains the jar and puts them all in the lib folder, including my own code

推荐答案

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

Try creating a custom assembly descriptor and add a dependencySet and make sure you specify, unpack as false.

将此作为汇编描述符使用,

Use this as assembly descriptor,

<?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>

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

Store this file to say src/main/assembly/assembly.xml and update your assembly plugin configuration in pom.xml like this.

        <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>

如果您还需要更多内容,这是汇编描述符引用

Here is assembly descriptor reference if you need anything more

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

这篇关于如何使用Maven创建具有完整依赖关系的独立应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆