2

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

  • 其中一些有 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>
4

1 回答 1

6

有很多选择供您选择

  1. 带有jar-with-dependencies描述符的Maven 程序集插件

  2. Maven One Jar插件

  3. Distribution Jar - 使用您的 jar 压缩存档,目录 lib 中的所有依赖项和配置的清单,以便于执行。下面的示例配置。

  4. 带有Maven Shade 插件的可执行 Jar

  5. 具有映射到本地 maven 存储库的依赖项的可运行 Jar(可能,但不可移植)更改类路径:使用 Maven 存储库样式的类路径

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

<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 组装插件

<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>
于 2013-11-11T16:37:15.337 回答