背景
這兩天打包一個(gè)Maven工程少办,這個(gè)工程同時(shí)依賴了:
- Maven中央倉(cāng)庫(kù)的包
- 本地的jar包
- JetBrains GUI Designer 生成的 form 文件
打包的時(shí)候遇到了一點(diǎn)麻煩,拋出各種class未定義的異常抄瑟,折騰了一會(huì)凡泣,最后發(fā)現(xiàn)處理的方式比較簡(jiǎn)單枉疼,不需要像這篇博客里加好幾個(gè)插件。
解決方法
如果我的項(xiàng)目依賴了項(xiàng)目根目錄/lib/yyy.jar
這個(gè)包鞋拟,Maven打包的時(shí)候不會(huì)把這個(gè)包加到依賴?yán)锩娴穆钗紫纫堰@個(gè)jar包聲明在pom.xml中:
<dependency>
<groupId>xxx.xxxx</groupId>
<artifactId>yyy</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/yyy.jar</systemPath>
</dependency>
記得替換上面的xxx和yyy。
然后在 pom.xml 中添加插件 maven-assembly-plugin
:
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- ... -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cn.edu.cqu.graphics.ui.Entrance</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/assembly/package.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
然后在項(xiàng)目的 src/assembly
目錄下添加一個(gè)descriptor文件package.xml
:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>
上面的配置跟https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
里的jar-with-dependencies
配置相比贺纲,多出了以下內(nèi)容:
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
作用就是把system scope
的依賴項(xiàng)加進(jìn)去航闺。
完成了以上配置后,運(yùn)行:
mvn clean compile assembly:single -e
就可以打包成功了猴誊。
關(guān)于 IntelliJ IDEA GUI Designer 的form文件依賴
我覺得最靠譜的方式還是讓IDE生成Java代碼:
設(shè)置成 Java Source Code后運(yùn)行一下潦刃,就會(huì)自動(dòng)生成form文件對(duì)應(yīng)的Java代碼。