IntelliJ GUI Designer Maven可执行JAR导出
我正在使用IntelliJ IDEA
的GUI设计器和Maven
作为构建系统。 当我通过这个答案构建可执行的JAR
文件时,构建成功。 但是,它通过命令java -jar MyApplication.jar
启动时抛出exception:
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 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:
4.0.0 groupId MyApplication 1.0-SNAPSHOT commons-lang commons-lang 2.6 org.jsoup jsoup 1.8.3 commons-io commons-io 2.4 commons-validator commons-validator 1.4.0 maven-assembly-plugin com.example.MyApplication BullyWiiPlaza jar-with-dependencies org.apache.maven.plugins maven-compiler-plugin 3.3 1.8 1.8 org.codehaus.mojo ideauidesigner-maven-plugin 1.0-beta-1 javac2 true true true
哪里不对? 如何使用Maven
与IntelliJ
的GUI设计器一起正确导出可执行JAR
文件?
使用IntelliJ IDEA 2017.1.5
我遇到了同样的问题,但我能够使用Maven
。 我在这里创建了一个带有更新的插件源代码的GitHub
存储库。
首先,克隆项目。
在ideauidesigner-maven-plugin-master
文件夹中,运行install-intellij-libs.sh
脚本以将IntelliJ
库安装到本地maven存储库中:
./install-intellij-libs.sh
这里还有一个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
的插件版本编辑为:
1.0-beta-2-17.1.5
还添加以下依赖项:
com.intellij javac2 LATEST com.intellij forms_rt LATEST com.intellij asm-all LATEST
现在构建应该与UI设计器表单一起正常工作。
这是因为Maven并不真正知道如何编译使用IntelliJ GUI Designer创建的GUI。 这就是为什么你必须明确指示IntelliJ生成它理解的所有代码而Maven没有。
要执行此操作,请转到GUI Designer设置,并将Generate GUI更改为Java Source文件的值。 从现在开始,IntelliJ将包含负责在类本身中设置UI的所有代码,并且所有外部工具(如Maven,Eclipse)都能正常工作。