IntelliJ GUI Designer Maven可执行JAR导出

时间:2015-09-23 19:26:49

标签: java maven intellij-idea jar

我使用IntelliJ IDEA的GUI设计器和Maven作为构建系统。当我通过this回答构建可执行文件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设计器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>

有什么问题?如何使用JARMaven的GUI设计器一起正确导出可执行文件IntelliJ

3 个答案:

答案 0 :(得分:10)

这是因为Maven并不真正知道如何编译使用IntelliJ GUI Designer创建的GUI。这就是为什么你必须明确指示IntelliJ生成它理解的所有代码而Maven没有。

要执行此操作,请转到GUI Designer设置,并将生成GUI 值更改为 Java源文件。从现在开始,IntelliJ将包含负责在类本身中设置UI的所有代码,并且所有外部工具(如Maven,Eclipse)都能正常工作。

答案 1 :(得分:7)

我使用IntelliJ IDEA 2017.1.5遇到了同样的问题,但我能够使用Maven。我创建了一个GitHub存储库,其中包含更新的插件源代码here

首先,克隆项目。

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设计器表单。

答案 2 :(得分:0)

我遇到了同样的问题,但我认为我找到了一个更简单的解决方案:

  1. 在IntelliJ中进入File -> Settings -> Editor -> GUI Designer并进行以下设置:

    • 将UI生成为:Java源代码
    • 启用Automatically copy form runtime classes...
  2. 在您的行家pom.xml中添加以下依赖项:

    <dependency>
        <groupId>com.intellij</groupId>
        <artifactId>forms_rt</artifactId>
        <version>7.0.3</version>
    </dependency>
    
  3. 另外将一个汇编插件片段添加到您的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>
    

应该这样做-至少对我有用。 请让我知道它是否对您也有用:-)