【问题标题】:IntelliJ GUI Designer Maven Executable JAR ExportIntelliJ GUI Designer Maven 可执行 JAR 导出
【发布时间】:2015-12-21 06:19:45
【问题描述】:

我使用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>

怎么了?如何使用Maven 结合IntelliJ 的GUI 设计器正确导出可执行的JAR 文件?

【问题讨论】:

  • 我遇到了同样的问题。你解决了吗?
  • @André: 不确定,但最近我不再有这个问题,一切顺利
  • 似乎问题出在 lambda 表达式上。如果我删除应用程序中的所有 lambda 表达式,它会部署....
  • @André:我也不使用 lambda 表达式...
  • 根据@André 的评论,请参阅:stackoverflow.com/questions/32135018/…

标签: java maven intellij-idea jar


【解决方案1】:

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

为此,请转到GUI Designer 设置并将Generate GUI into 值更改为Java Source files。从现在开始,IntelliJ 将包含负责在类本身中设置 UI 的所有代码以及所有外部工具,如 Maven,Eclipse 将正常工作。

【讨论】:

    【解决方案2】:

    我在使用 IntelliJ IDEA 2017.1.5 时遇到了同样的问题,但我能够使用 Maven 让它工作。我使用更新的插件源代码here 创建了一个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 版本的兼容性修复。在 Windows 8(希望是 10)下编译它之后,在使用 IntelliJ-GUI-Editor 和 maven-plugin 时,我可以再次使用 lambdas。
    • 有效。一件奇怪的事情是,在运行 mvn package 之后,IntelliJ 会在您尝试使用 IDE 运行项目时抛出以下异常:Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/intellij/uiDesigner/core/Spacer 如何避免以及如何避免此异常?通过Build -> Rebuild Project 重建是一个修复,但为什么插件会导致丢失类?
    【解决方案3】:

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

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

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

      <dependency>
          <groupId>com.intellij</groupId>
          <artifactId>forms_rt</artifactId>
          <version>7.0.3</version>
      </dependency>
      
    3. 另外添加到你的pom.xml 一个汇编插件sn-p,它可能看起来像这个例子:

          <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>
      

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

    【讨论】:

    • 请问如何将此解决方案应用于 Gitlab CI/CD?我遵循了您的回答,但不幸的是,它不起作用。在修复之前,我有与 OP 相同的错误消息。提前致谢。
    【解决方案4】:

    @jorichard 的The answer 可以简化并使 CI 友好 - 因此无需本地安装任何 IDE,所有配置和依赖项管理均由 Maven 完成。

    插件org.codehaus.mojo:ideauidesigner-maven-plugin:1.0-beta-1 对 Jetbrains 通过com.intellij:javac2:7.0.3com.intellij:forms_rt:7.0.3 提供的代码有两个依赖项(最后一次更新是在 2008 年)。从那以后这些都没有直接更新,但在their own repositories 中由 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 2012-06-21
      • 2013-12-23
      • 1970-01-01
      相关资源
      最近更新 更多