如何将pom项目打包为runnable jar

时间:2013-11-11 16:16:19

标签: java maven jar

我有一个基于免费和开源程序的免费项目:

  • 其中一些人拥有pom和公共无障碍罐子
  • 其中一些人有pom但没有公共无障碍罐子
  • 其中一些人没有pom

我想将项目作为一个独特的jar分发,因为我合法地可以兼容所有许可证,如果有可能用maven生成这样的jar,我不知道如何。你能帮忙吗?

如果不可能分发此类项目的最佳方式是什么?


我的pom由netbeans管理,这是缺少的配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <!-- I added the following -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>net.hypermove.graphitidb.GraphitiDB</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

1 个答案:

答案 0 :(得分:5)

您有很多选择

  1. Maven Assembly Pluginjar-with-dependencies描述符

  2. Maven One Jar插件

  3. 分发Jar - Zip存档与您的jar,目录lib中的所有依赖项和配置清单,以便于执行。下面的示例配置。

  4. Executable JarMaven Shade Plugin

  5. Runnable Jar,其依赖关系映射到本地maven存储库(可能,但不可移植)Altering The Classpath: Using a Maven Repository-Style Classpath

  6. 1。 Jar与依赖性组合

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>make-jar-with-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>eu.codearte.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    2。 Maven One Jar

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>eu.codearte.MainClass</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.dstovall</groupId>
        <artifactId>onejar-maven-plugin</artifactId>
        <version>1.4.4</version>
        <executions>
            <execution>
                <goals>
                    <goal>one-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    ...
    <pluginRepositories>
        <pluginRepository>
            <id>onejar-maven-plugin.googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
    </pluginRepositories>
    

    3。分发Jar配置

    Maven依赖插件

    <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <overWriteReleases>true</overWriteReleases>
                    <overWriteSnapshots>true</overWriteSnapshots>
                    <overWriteIfNewer>true</overWriteIfNewer>
                    <stripVersion>false</stripVersion>
                    <stripClassifier>false</stripClassifier>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Maven Assembly Plugin

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/bundle.xml</descriptor>
                    </descriptors>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Maven Jar插件

    <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
           <execution>
                <id>exe-jar</id>
                <goals>
                    <goal>jar</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                    <classifier>exe</classifier>
                    <archive>
                        <manifest>
                            <mainClass>eu.codearte.MainClass</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    装配描述符

    <?xml version='1.0' encoding='UTF-8'?>
    <assembly
            xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
        <id>bundle</id>
        <formats>
            <format>zip</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <fileSet>
                <directory>target/lib</directory>
                <outputDirectory>lib</outputDirectory>
            </fileSet>
        </fileSets>
        <files>
            <file>
                <source>target/project-exe.jar</source>
                <outputDirectory>/</outputDirectory>
            </file>
        </files>
    </assembly>