[英]IntelliJ GUI Designer Maven Executable JAR Export
我使用IntelliJ IDEA
的 GUI 設計器和Maven
作為構建系統。 當我通過此答案構建可執行JAR
文件時,構建成功。 但是,它在通過命令java -jar MyApplication.jar
啟動時會引發異常:
Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:621)
at javax.swing.JFrame.setContentPane(JFrame.java:698)
...
受影響的代碼行如下:
setContentPane(panel);
從IntelliJ
中的源代碼運行時,它可以正常工作,但是Maven
似乎無法正確構建JAR
文件。 畢竟,IntelliJ 通過鏈接到.form
文件來保持.java
源代碼文件不受 GUI 代碼影響,從而實現了“魔法”。
我還找到了一個可能的解決方案,它涉及在pom.xml
文件中添加一個特殊插件,該插件似乎可以在此處啟用對IntelliJ
的 GUI 設計器的構建支持。 所以我再次運行mvn clean compile assembly:single
,它沒有任何錯誤但是沒有任何改變。
如果我執行mvn deploy
,插件會拋出以下錯誤:
[ERROR] Failed to execute goal org.codehaus.mojo:ideauidesigner-maven-plugin:1.0-beta-1:javac2 (default) on project MyApplication: Execution default of goal org.codehaus.mojo:ideauidesigner-maven-plugin:1.0-beta-1:javac2 failed: 16257 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
這是我的 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
<artifactId>MyApplication</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Apache Commons Lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Jsoup HTML parser -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<!-- Apache Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- Apache Commons Validator -->
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.MyApplication
</mainClass>
</manifest>
<manifestEntries>
<Built-By>BullyWiiPlaza</Built-By>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- IDEA Gui Designer Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>ideauidesigner-maven-plugin</artifactId>
<version>1.0-beta-1</version>
<executions>
<execution>
<goals>
<goal>javac2</goal>
</goals>
</execution>
</executions>
<configuration>
<fork>true</fork>
<debug>true</debug>
<failOnError>true</failOnError>
</configuration>
</plugin>
</plugins>
</build>
</project>
怎么了? 如何使用Maven
結合IntelliJ
的 GUI 設計器正確導出可執行JAR
文件?
發生這種情況是因為 Maven 並不真正知道如何編譯使用 IntelliJ GUI Designer 創建的 GUI。 這就是為什么您必須明確指示 IntelliJ 生成它可以理解的所有代碼而 Maven 不能。
為此,請轉到GUI Designer設置並將Generate GUI into value 更改為Java Source files 。 從現在開始,IntelliJ 將包括所有負責在類本身中設置 UI 的代碼,以及所有外部工具,如 Maven,Eclipse 將正常工作。
我在使用IntelliJ IDEA 2017.1.5
時遇到了同樣的問題,但我能夠讓它與Maven
一起使用。 我在這里使用更新的插件源代碼創建了一個GitHub
存儲庫。
首先,克隆項目。
在ideauidesigner-maven-plugin-master
文件夾中,運行install-intellij-libs.sh
腳本將IntelliJ
庫安裝到本地 maven 存儲庫中:
./install-intellij-libs.sh <path to your IntelliJ directory>
這里還有一個適用於 Windows 的批處理文件 ( install-intellij-libs.bat
):
SET INTELLIJ_HOME=C:\Program Files\JetBrains\IntelliJ IDEA 173.3188.16 REM Edit this to match your path!
CALL mvn install:install-file -Dfile="%INTELLIJ_HOME%\lib\javac2.jar" -DgroupId=com.intellij -DartifactId=javac2 -Dversion=17.1.5 -Dpackaging=jar
CALL mvn install:install-file -Dfile="%INTELLIJ_HOME%\lib\asm-all.jar" -DgroupId=com.intellij -DartifactId=asm-all -Dversion=17.1.5 -Dpackaging=jar
CALL mvn install:install-file -Dfile="%INTELLIJ_HOME%\lib\forms_rt.jar" -DgroupId=com.intellij -DartifactId=forms_rt -Dversion=17.1.5 -Dpackaging=jar
然后通過運行以下命令安裝新插件:
mvn install
現在您已經完成了環境的設置。
在您的實際項目中,將pom.xml
中的插件版本編輯為:
<version>1.0-beta-2-17.1.5</version>
還要添加以下依賴項:
<dependency>
<groupId>com.intellij</groupId>
<artifactId>javac2</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>asm-all</artifactId>
<version>LATEST</version>
</dependency>
現在構建應該與 UI 設計器表單一起正常工作。
我有同樣的問題,但我想我找到了一個更簡單的解決方案:
在 IntelliJ 中進入File -> Settings -> Editor -> GUI Designer
並執行以下設置:
Automatically copy form runtime classes...
將以下依賴項添加到 yout maven pom.xml
中:
<dependency> <groupId>com.intellij</groupId> <artifactId>forms_rt</artifactId> <version>7.0.3</version> </dependency>
此外,在您的pom.xml
中添加一個程序集插件片段,如下所示:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <finalName>testUiClient</finalName> <appendAssemblyId>false</appendAssemblyId> <archive> <manifest> <mainClass> my.personal.MainClass </mainClass> </manifest> <manifestEntries> <Multi-Release>true</Multi-Release> <Class-Path>.</Class-Path> </manifestEntries> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </execution> </executions> </plugin> </plugins> </build>
應該這樣做 - 它至少對我有用。 讓我知道它是否也適合你:-)
@jorichard的回答可能會被簡化並使 CI 友好——因此不需要本地安裝任何 IDE,所有配置和依賴項管理都由 Maven 完成。
插件org.codehaus.mojo:ideauidesigner-maven-plugin:1.0-beta-1
對 Jetbrains 通過com.intellij:javac2:7.0.3
和com.intellij:forms_rt:7.0.3
提供的代碼有兩個依賴項(兩者都是最后更新於 2008 年)。 從那以后,這些都沒有直接更新,但在他們自己的存儲庫中擁有由 Jetbrains 托管的現代等價物。
要做的就是覆蓋插件依賴項。 在此配置中(對我有用),我更新到版本212.5284.40
的 jetbrains 庫,或在撰寫本文時最新版本,對應於 Intellij IDEA 2021.2.2。
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>ideauidesigner-maven-plugin</artifactId>
<version>1.0-beta-1</version>
<dependencies>
<dependency>
<groupId>com.jetbrains.intellij.java</groupId>
<artifactId>java-compiler-ant-tasks</artifactId>
<version>212.5284.40</version>
</dependency>
<dependency>
<groupId>com.jetbrains.intellij.java</groupId>
<artifactId>java-gui-forms-rt</artifactId>
<version>212.5284.40</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>javac2</goal>
</goals>
</execution>
</executions>
<configuration>
<fork>true</fork>
<debug>true</debug>
<failOnError>true</failOnError>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>intellij-repository</id>
<url>https://www.jetbrains.com/intellij-repository/releases</url>
</pluginRepository>
<pluginRepository>
<id>intellij-third-party</id>
<url>https://cache-redirector.jetbrains.com/intellij-dependencies</url>
</pluginRepository>
</pluginRepositories>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.