如何在Maven中构建SWT应用并打包成可执行的jar包

    前面在Maven中构建SWT应用的时候发现SWT相关jar包在Maven中央仓库上找不到,后面在stackoverflow上有人提供了一个仓库地址:https://github.com/maven-eclipse/maven-eclipse.github.io

    根据上面所说,SWT相关依赖在Maven中央仓库中不可取,目前存在的Maven仓库不包括所有平台的包,也没有源码和debug包,总之就是没有提供可信赖和自动化的方式更新和仓库再生,而http://maven-eclipse.github.io/maven这个仓库会自动下载并打包官方的SWT发布版本。

一、如何添加swt相关依赖

在pom.xml中添加下面的仓库地址:

<repositories>
	<repository>
		<id>maven-eclipse-repo</id>
		<url>http://maven-eclipse.github.io/maven</url>
	</repository>
</repositories>

下面是各平台的依赖:

<dependencies>
	<dependency>
		<groupId>org.eclipse.swt</groupId>
		<artifactId>org.eclipse.swt.win32.win32.x86</artifactId>
		<version>${swt.version}</version>
		<!-- To use the debug jar, add this -->
		<classifier>debug</classifier>
	</dependency>
	<dependency>
		<groupId>org.eclipse.swt</groupId>
		<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
		<version>${swt.version}</version>
	</dependency>
	<dependency>
		<groupId>org.eclipse.swt</groupId>
		<artifactId>org.eclipse.swt.gtk.linux.x86</artifactId>
		<version>${swt.version}</version>
	</dependency>
	<dependency>
		<groupId>org.eclipse.swt</groupId>
		<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
		<version>${swt.version}</version>
	</dependency>
	<dependency>
		<groupId>org.eclipse.swt</groupId>
		<artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>
		<version>${swt.version}</version>
	</dependency>
</dependencies>

二、如何通过Maven打包成可执行的jar并打包依赖

这里要用到maven-assembly-plugin插件而不是maven-jar-plugin,maven-jar-plugin并不会打包依赖,而通过maven-assembly-plugin可以打包依赖、组件、文档和其它文件到jar中,具体参考Maven官网关于assembly插件的介绍:https://maven.apache.org/plugins/maven-assembly-plugin

在pom.xml中添加assembly插件:

<build>
    <finalName>${project.name}</finalName>
	<plugins>
		<plugin>
			<!-- 这里不需要指定groupId,默认为org.apache.maven.plugins -->
			<artifactId>maven-assembly-plugin</artifactId>
			<version>3.1.0</version>
			<configuration>
				<archive>
					<manifest>
						<addClasspath>true</addClasspath>
						<mainClass>com.iboxpay.ui.MainWindow</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>
</build>

注意:

<descriptorRef>jar-with-dependencies</descriptorRef>标签中的内容必须为jar-with-dependencies,否则打包时会出错,若想自定义配置描述符,参考https://maven.apache.org/plugins/maven-assembly-plugin/usage.html

配完后运行maven package会生成连个包,一个通过maven-jar-plugin打包生成的"工程名.jar",另一个是通过maven-assembly-plugin打包生成的"工程名-jar-with-dependencies.jar"。后者可通过java -jar  工程名-jar-with-dependencies.jar 直接运行。

猜你喜欢

转载自blog.csdn.net/lingbomanbu_lyl/article/details/84066565
今日推荐