如何使用maven程序集将类路径属性添加到可执行jar

时间:2014-08-28 04:52:05

标签: java eclipse maven jar

我使用以下插件生成可执行jar。我在我的类路径中有一个属性文件,我想添加到我的jar,但是使用程序集插件我无法做到这一点。我找到了一些答案说我们可以使用jar插件创建一个jar,然后使用assembly插件打包它们。可以任何人帮助我如何做到这一点。

我的插件说明

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>mainclass</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
                <includes>
                <include>myconfig.propeties</include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <finalName>app</finalName>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>
                            mainclass
                        </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>
                        jar-with-dependencies
                    </descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

我的jar-with-dependeinceis文件

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>bin</id>
<!-- Generates a zip package containing the needed files -->
<formats>
<format>zip</format>
</formats>

<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
    <dependencySet>
        <!--
           Project artifact is not copied under library directory since
           it is added to the root directory of the zip package.
       -->
        <useProjectArtifact>false</useProjectArtifact>
        <outputDirectory>lib</outputDirectory>
        <unpack>false</unpack>
    </dependencySet>
</dependencySets>

<fileSets>
    <!--
       Adds startup scripts to the root directory of zip package. The startup
       scripts are located to src/main/scripts directory as stated by Maven
       conventions.
   -->
    <fileSet>
        <directory>${project.build.scriptSourceDirectory}</directory>
        <outputDirectory></outputDirectory>
        <includes>
            <include>startup.*</include>
        </includes>
    </fileSet>
    <!-- adds jar package to the root directory of zip package -->
    <fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory></outputDirectory>
        <includes>
            <include>*.jar</include>
        </includes>
    </fileSet>
</fileSets>

提前感谢。

1 个答案:

答案 0 :(得分:2)

我通常发现使用阴影插件更容易。它将运行jar所需的所有依赖项捆绑到一个jar中,因此您不必修改类路径,所有内容都在jar中:

http://maven.apache.org/plugins/maven-shade-plugin/

使用示例:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <minimizeJar>true</minimizeJar>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>your.package.Main</mainClass>
                    </transformer>
                </transformers>
                <artifactSet>
                    <excludes>
                        <!-- maybe there is stuff you don't want in the jar -->
                        <exclude>log4j:log4j</exclude>
                        <exclude>org.slf4j:slf4j-api</exclude>
                        <exclude>org.slf4j:slf4j-log4j12</exclude>
                    </excludes>
                </artifactSet>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>