2015-09-23 69 views
4

我使用IntelliJ IDEA的GUI設計和Maven作爲構建系統。當我通過this答案構建可執行文件JAR時,構建成功。然而,它拋出通過命令java -jar MyApplication.jar啓動時的異常:的IntelliJ GUI設計的Maven可執行的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設計器here的構建支持。所以我再次跑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

+1

我遇到了同樣的問題。你解決了這個問題嗎? –

+0

@André: 不知道,但最近我不再有這個問題,它一切似乎 – BullyWiiPlaza

+2

看起來像問題是lambda表達式。如果我刪除我的應用程序中的所有lambda表達式,它將部署.... –

回答

5

我不得不使用IntelliJ IDEA 2017.1.5同樣的問題,但我能得到它Maven工作。我使用更新後的插件源代碼here創建了一個GitHub存儲庫。

首先,克隆項目。

ideauidesigner-maven-plugin-master文件夾,運行install-intellij-libs.sh腳本到IntelliJ庫安裝到你的本地倉庫:

./install-intellij-libs.sh <path to your IntelliJ directory> 

這裏也是一個批處理文件(install-intellij-libs.bat)的Windows:

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設計師形式的正常工作。

+0

我想感謝您和原始維護人員對於新版IntelliJ-Versions的兼容性修補程序的源代碼。在Windows 8下編譯它(希望是10)後,我可以在使用IntelliJ-GUI-Editor和maven-plugin時再次使用lambdas。 – user3469861

+0

作品。一個奇怪的事情是,在運行'mvn package'後,當你嘗試使用IDE運行你的項目時,'IntelliJ'會拋出以下異常:'線程中的異常AWT-EventQueue-0「java.lang.NoClassDefFoundError:com/intellij/uiDesigner/core/Spacer'怎麼樣以及如何避免這種異常?通過'Build' - >'Rebuild Project'重建是一個解決方案,但爲什麼插件會導致缺失的類? – BullyWiiPlaza

4

發生這種情況是因爲Maven並不真正知道如何編譯使用IntelliJ GUI Designer創建的GUI。這就是爲什麼你必須明確指示IntelliJ生成所有它理解的代碼,而Maven則不會。

要做到這一點去GUI Designer設置和更改生成GUI爲Java源文件。從現在開始,IntelliJ將包含所有負責在類中設置UI的代碼,並且所有外部工具(如Maven,Eclipse)都能正常工作。