简体   繁体   English

使用Maven,Eclipse构建可执行jar

[英]Building an executable jar with Maven, Eclipse

End goal is I am trying to build an executable jar in eclipse with maven, that's packaged with its dependencies like so. 最终目标是我试图使用maven在eclipse中构建一个可执行jar,并与它的依赖项一起打包。 The end file structure I'm trying to accomplish is the following.. 我要完成的最终文件结构如下。

/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

From here the end user would be able to unzip the distributable zip file, double click on the jar to start a java swing application which has dependencies located in the /lib directory. 最终用户可以从此处解压缩可分发的zip文件,双击jar来启动一个Java swing应用程序,该应用程序的依赖项位于/ lib目录中。 And there's a text editor in/as part of the swing app that I'd like to have reference images from the /images directory. 在swing应用程序中/作为其一部分,有一个文本编辑器,我想从/ images目录中获取参考图像。 What I would like to accomplish is having maven create a zip file which contains everything in this /dist folder, including my executable jar and it's dependencies located in a lib directory. 我要完成的工作是让maven创建一个zip文件,其中包含此/ dist文件夹中的所有内容,包括我的可执行jar和它的依赖项(位于lib目录中)。

I know I need to have my exec jar's manifest include the Main class from which to run the application, and include the lib and images directory on the classpath of the manifest as well as my properties file. 我知道我需要让我的exec jar的清单包含运行该应用程序的Main类,并在清单的类路径以及我的属性文件中包含lib和images目录。

I have tried several combinations of the maven shade plugin which seems to throw everything in one jar (not useful here as I need the zip with my exec jar inside, and lib outside the exec jar), and maven-assembly-plugin. 我已经尝试了几种组合的maven shade插件,似乎将所有东西都扔进了一个jar中(这里没有用,因为我需要将exec jar放在里面的zip压缩文件,而exec jar放在外面的lib)和maven-assembly-plugin。

Here is my current POM with dependencies.. 这是我当前带有依赖项的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>

And this is my last attempt with using the maven-assembly-plugin adding to the pom above.. 这是我最后一次使用将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>

And my 和我的

assembly.xml 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>

My application structure.. 我的应用程序结构

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

When I try to use the pom above with the build section using the maven-assembly-plugin I'm currently getting the error.. 当我尝试使用maven-assembly-plugin将以上pom与build部分一起使用时,我目前遇到错误。

Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2.1:single (default) on project kiosk-application: Error reading assemblies: Error reading descriptor: /src/main/assembly/assembly.xml: Unrecognised tag: 'filesets' (position: START_TAG seen ...\\r\\n ... @8:15) -> [Help 1] 无法在项目亭应用程序上执行目标org.apache.maven.plugins:maven-assembly-plugin:2.2.1:single(默认):错误读取程序集:错误读取描述符:/src/main/assembly/assembly.xml :无法识别的标签:“文件集”(位置:看到了START_TAG ... \\ r \\ n ... @ 8:15)-> [帮助1]

This seems like what should be a fairly easy task to accomplish, however I have never used maven to accomplish this sort of thing and I'm having an issue orchestrating the build to produce the end result I'd like to accomplish. 这似乎应该是一件很容易完成的任务,但是我从来没有用过Maven来完成这种事情,而且在编排构建以产生最终想要完成的结果时遇到了问题。 Is there any maven experts out there than can help point me in the right direction..? 有没有能帮助我指出正确方向的专家专家?

You should be using fileSets instead of filesets in your assembly.xml. 您应该在fileSets中使用filesets而不是filesets集。 Note uppercase 'S' letter. 注意大写的“ S”字母。 The same applies to several other XML tags. 这同样适用于其他几个XML标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM