[英]Building an executable jar with Maven, Eclipse
最终目标是我试图使用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。
<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 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与build部分一起使用时,我目前遇到错误。
无法在项目亭应用程序上执行目标org.apache.maven.plugins:maven-assembly-plugin:2.2.1:single(默认):错误读取程序集:错误读取描述符:/src/main/assembly/assembly.xml :无法识别的标签:“文件集”(位置:看到了START_TAG ... \\ r \\ n ... @ 8:15)-> [帮助1]
这似乎应该是一件很容易完成的任务,但是我从来没有用过Maven来完成这种事情,而且在编排构建以产生最终想要完成的结果时遇到了问题。 有没有能帮助我指出正确方向的专家专家?
您应该在fileSets
中使用filesets
而不是filesets
集。 注意大写的“ S”字母。 这同样适用于其他几个XML标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.