使用Maven,Eclipse构建可执行jar

时间:2014-03-04 19:29:29

标签: java maven executable-jar maven-assembly-plugin

最终目标是我正在尝试使用maven在eclipse中构建一个可执行jar,它与其依赖项一样打包。我想要完成的结束文件结构如下..

/target/my-distributable.zip
        |- lib (all third party jar's, which I have loaded through maven)
        |- images (the images my swing app can reference)
        |- my-application.jar (my executable jar, manifiest within with correct classpath references)
        |- my-application.properties (the properties my app reads and can modify)
        |- READ-ME.TXT

从这里开始,最终用户可以解压缩可分发的zip文件,双击jar来启动一个java swing应用程序,该应用程序具有位于/ lib目录中的依赖项。作为swing应用程序的一部分,我有一个文本编辑器,我想从/ images目录中获取参考图像。我想要完成的是让maven创建一个zip文件,其中包含此/ dist文件夹中的所有内容,包括我的可执行jar及其位于lib目录中的依赖项。

我知道我需要让我的exec jar的清单包含运行应用程序的Main类,并在清单的类路径中包含lib和images目录以及我的属性文件。

我已经尝试过maven shade插件的几个组合,它们似乎把所有内容都放在一个jar中(这里没用,因为我需要带有我的exec jar的zip,以及exec jar之外的lib),以及maven-assembly-plugin

这是我当前的具有依赖关系的POM ..

<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.kiosk</groupId>
  <artifactId>kiosk-application</artifactId>
  <version>2.1</version>

  <name>Kiosk Application</name>

    <properties>
        <jdk.version>1.5</jdk.version>
    </properties>

  <dependencies>

    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.1</version>
    </dependency>

    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>

    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>commons-jxpath</groupId>
        <artifactId>commons-jxpath</artifactId>
        <version>1.3</version>
    </dependency>

    <dependency>
        <groupId>net.sf.ezmorph</groupId>
        <artifactId>ezmorph</artifactId>
        <version>1.0.6</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.0</version>
    </dependency>

    <dependency>
        <groupId>com.miglayout</groupId>
        <artifactId>miglayout</artifactId>
        <version>3.7.4</version>
    </dependency>

    <dependency>
        <groupId>com.shef</groupId>
        <artifactId>shef</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>com.novaworx.syntax</groupId>
        <artifactId>novaworx-syntax</artifactId>
        <version>0.0.7</version>
    </dependency>

    <dependency>
        <groupId>sam.i.am</groupId>
        <artifactId>sam-i-am</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>jtidy.me</groupId>
        <artifactId>jtidy-8</artifactId>
        <version>8.0</version>
    </dependency>

  </dependencies>
</project>

这是我最后一次尝试使用maven-assembly-plugin添加到上面的pom中。

  <build>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <versionRange>[2.0,)</versionRange>
                    <goals>
                      <goal>copy-dependencies</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <execute />
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                  <source>1.5</source>
                  <target>1.5</target>
                </configuration>
            </plugin>

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <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}/libs</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <compress>true</compress>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>libs/</classpathPrefix>
                            <mainClass>com.kiosk.app.KioskApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>



           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>/src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                    <finalName>kiosk-application_${project.version}</finalName>
                    <outputDirectory>${project.build.directory}/dist</outputDirectory>
                    <workDirectory>${project.build.directory}/assembly/work</workDirectory>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

    </plugins>

  </build>

和我的

assembly.xml

<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>jar</format>
    </formats>
    <filesets>
        <fileset>
            <directory>target</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileset>
        <fileset>
            <directory>target/libs</directory>
            <outputDirectory>libs</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileset>
    </filesets>
</assembly>

我的申请结构..

/src
 |- main
    |- assembly
    |- java
       |- com
          |- kiosk
             |- etc....
    |- resources
       |- myImage.jpg
       |- my-application.properties

当我尝试使用maven-assembly-plugin在构建部分使用上面的pom时,我当前收到错误..

无法执行目标org.apache.maven.plugins:maven-assembly-plugin:2.2.1:单一(默认)项目kiosk-application:读取程序集时出错:读取描述符时出错:/ src / main / assembly / assembly.xml:无法识别的标签:'filesets'(位置:START_TAG见...... \ r \ n ... @ 8:15) - &gt; [帮助1]

这看起来应该是一个相当容易完成的任务,但是我从来没有使用过maven来完成这类事情而且我有一个问题,编排构建以产生我想要完成的最终结果。是否有任何专家专家可以帮我们指出正确的方向..?

1 个答案:

答案 0 :(得分:1)

您应该在assembly.xml中使用fileSets而不是filesets。注意大写的'S'字母。这同样适用于其他几个XML标记。