Maven IDEA UI Designer Plugin Error [duplicate] - java

I'm using IntelliJ IDEA's GUI designer and Maven as build system. When I build the executable JAR file via this answer, the build succeeds. However, it throws an exception when launched via the command 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)
...
The affected code line is the following:
setContentPane(panel);
When ran from source in IntelliJ, it works fine however Maven doesn't seem to properly build the JAR file. After all, IntelliJ does "magic" by linking to a .form file for keeping .java source code files clean from GUI code.
I also found a possible solution which involves adding a special plugin to the pom.xml file that seems to enable build support for IntelliJ's GUI designer here. So I ran mvn clean compile assembly:single again, it didn't have any errors however nothing changed.
If I do a mvn deploy, the plugin throws the following error:
[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
Here is my 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>
What is wrong? How do I properly export an executable JAR file using Maven in combination with IntelliJ's GUI designer?

That happens because Maven doesn't really know how to compile GUI created with IntelliJ GUI Designer. That's why you have to explicitly instruct IntelliJ to generate all the code it understands and Maven doesn't.
To do this go to GUI Designer settings and change the Generate GUI into value to Java Source files. From now on IntelliJ will include all the code responsible for setting UI up in the class itself and all the external tools, like Maven, Eclipse will work properly.

I had the same problem using IntelliJ IDEA 2017.1.5 but I was able to get it working with Maven. I created a GitHub repository with the updated plugin source code here.
First, clone the project.
In the ideauidesigner-maven-plugin-master folder, run the install-intellij-libs.sh script to install the IntelliJ libraries into your local maven repository:
./install-intellij-libs.sh <path to your IntelliJ directory>
Here is also a batch file (install-intellij-libs.bat) for 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
Then install your new plugin by running the following:
mvn install
Now you're done with setting up your environment.
In your actual project, edit the plugin's version in your pom.xml to this:
<version>1.0-beta-2-17.1.5</version>
Also add the following dependencies:
<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>
Now building should work correctly with UI designer forms.

I had the same problem but I think I found a much easier solution:
In IntelliJ go into File -> Settings -> Editor -> GUI Designer and do the following setting:
Generate UI into: Java source code
Enable Automatically copy form runtime classes...
Add the following dependncy into yout maven pom.xml:
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>7.0.3</version>
</dependency>
Additionally add to your pom.xml an assembly plugin snippet which could look like this example:
<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>
That should do it - it works at least for me.
Just let me know if it works also for you :-)

The answer by #jorichard may be simplifed and made CI friendly - so no local installation of any IDE is required and all configuration and dependency management is done by Maven.
The plugin org.codehaus.mojo:ideauidesigner-maven-plugin:1.0-beta-1 has 2 dependencies on the code provided by Jetbrains via com.intellij:javac2:7.0.3 and com.intellij:forms_rt:7.0.3 (both last updated in 2008). These have not been directly updated since, but have modern equivalents hosted by Jetbrains in their own repositories.
All there is to be done is to overwrite the plugin dependencies. In this configuration (which is working for me) I updated to jetbrains libraries in version 212.5284.40, or latest at the time of writing, corresponding to 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>

Related

JavaFX + Maven + Ojdbc6 + IntelliJ (deploy oracle jar with Javafx application for windows)

I've been trying to deploy my JavaFX application with the Oracle client (ojdbc6) embedded into the same jar.
The application runs fine and connects to the database if I'm running it through IntelliJ, but once I run the "package" task and try and run the application from double clicking the jar or running:
"java -jar .\maven-Newton-1.0-SNAPSHOT.jar"
The application starts, but it won't connect to the DB:
`
In the code I've tried both:
Class.forName("oracle.jdbc.driver.OracleDriver");
and
Class.forName("oracle.jdbc.OracleDriver");
I'm just starting with maven and I'm not too sure if my configurations are correct:
• I've tried adding the ojdbc6.jar to the global libraries:
• I've tried adding the ojdbc6.jar file to the SDK classpath:
• And I've messed around with the module dependencies:
But my problem may be lying on the POM file because the other jar that I want to embed within my application is not deploying as well (org.reflections)
• 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>Newton</groupId>
<artifactId>maven-Newton</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Controller.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
</dependencies>
</project>
Not too sure where to go from here, any help would be very much appreciated.
Thanks
The problem is that the single jar file, you produce with the 'package' goal does not contain the ojdbc.jar in itself.
You will have to run the jar file with a classpath, e.g.
java -cp ojdbc.jar;othernecessary.jar maven-newton-project-1.0.jar
Btw, there is a distinction in maven between dependencies (which are needed for the code to work) and plugins (which are needed for maven to build). You dependency on maven-compiler-plugin and maven-resource-plugin suggests to me you are confusing these two concepts.
I would move these two into the section called <build><plugins> instead alongside the maven-jar-plugin.
If you want an easy to comprehend start, try this: http://www.darrencoxall.com/java/understanding-maven/

IntelliJ GUI Designer Maven Executable JAR Export

I'm using IntelliJ IDEA's GUI designer and Maven as build system. When I build the executable JAR file via this answer, the build succeeds. However, it throws an exception when launched via the command 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)
...
The affected code line is the following:
setContentPane(panel);
When ran from source in IntelliJ, it works fine however Maven doesn't seem to properly build the JAR file. After all, IntelliJ does "magic" by linking to a .form file for keeping .java source code files clean from GUI code.
I also found a possible solution which involves adding a special plugin to the pom.xml file that seems to enable build support for IntelliJ's GUI designer here. So I ran mvn clean compile assembly:single again, it didn't have any errors however nothing changed.
If I do a mvn deploy, the plugin throws the following error:
[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
Here is my 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>
What is wrong? How do I properly export an executable JAR file using Maven in combination with IntelliJ's GUI designer?
That happens because Maven doesn't really know how to compile GUI created with IntelliJ GUI Designer. That's why you have to explicitly instruct IntelliJ to generate all the code it understands and Maven doesn't.
To do this go to GUI Designer settings and change the Generate GUI into value to Java Source files. From now on IntelliJ will include all the code responsible for setting UI up in the class itself and all the external tools, like Maven, Eclipse will work properly.
I had the same problem using IntelliJ IDEA 2017.1.5 but I was able to get it working with Maven. I created a GitHub repository with the updated plugin source code here.
First, clone the project.
In the ideauidesigner-maven-plugin-master folder, run the install-intellij-libs.sh script to install the IntelliJ libraries into your local maven repository:
./install-intellij-libs.sh <path to your IntelliJ directory>
Here is also a batch file (install-intellij-libs.bat) for 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
Then install your new plugin by running the following:
mvn install
Now you're done with setting up your environment.
In your actual project, edit the plugin's version in your pom.xml to this:
<version>1.0-beta-2-17.1.5</version>
Also add the following dependencies:
<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>
Now building should work correctly with UI designer forms.
I had the same problem but I think I found a much easier solution:
In IntelliJ go into File -> Settings -> Editor -> GUI Designer and do the following setting:
Generate UI into: Java source code
Enable Automatically copy form runtime classes...
Add the following dependncy into yout maven pom.xml:
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>7.0.3</version>
</dependency>
Additionally add to your pom.xml an assembly plugin snippet which could look like this example:
<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>
That should do it - it works at least for me.
Just let me know if it works also for you :-)
The answer by #jorichard may be simplifed and made CI friendly - so no local installation of any IDE is required and all configuration and dependency management is done by Maven.
The plugin org.codehaus.mojo:ideauidesigner-maven-plugin:1.0-beta-1 has 2 dependencies on the code provided by Jetbrains via com.intellij:javac2:7.0.3 and com.intellij:forms_rt:7.0.3 (both last updated in 2008). These have not been directly updated since, but have modern equivalents hosted by Jetbrains in their own repositories.
All there is to be done is to overwrite the plugin dependencies. In this configuration (which is working for me) I updated to jetbrains libraries in version 212.5284.40, or latest at the time of writing, corresponding to 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>

Maven: Generating an executable jar file with all the dependencies

I recently converted one of my Java project to a Maven project. My pom.xml looks something like this:
<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>
<packaging>jar</packaging>
<groupId>com.myproject.groupid</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyProject</name>
<description>The first version of my maven project</description>
<dependencies>
<dependency>
<groupId>com.dependent.jar</groupId>
<artifactId>dependentjar</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<type>jar</type>
<systemPath>${project.basedir}/jars/dependent.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.myproject.main.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</build>
</project>
When i execute the mvn compile and mvn install, the project works fine and it also generates the jar. But when i try to run the jar [using java -jar MyProject.jar], i get an error which says: Exception in thread "main" java.lang.NoClassDefFoundError and this is because maven is not able to add the dependent jar specified in the section. [it is not available during run time]
Could anybody let me know the best possible way for me to copy the systemPath jars to the jar that is being generated by maven?
I looked at maven-shade-plugin and maven-assembly-plugin and could not find much luck with both of them. Any help would be appreciated!
Try to add this to your maven-jar-plugin configuration
<addClasspath>true</addClasspath>
<classpathPrefix>*path to dependencies*</classpathPrefix>
Also to manage your dependencies you can use Maven dependency plugin
In the build section of your pom, you can use:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com:library:**</include>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then, on the command line, you can run mvn package, it will show in the output which jars it is excluding. Then, you can include selected jars accordingly. If you then get NoClassDefFoundError, you may need to point Java to your class, by for example using com.company.Main as an argument to the java command.

Apache Spark building on Amazon EC2 error with Spark-Maven-Plugin failure

I am currently building Apache Spark on Amazon EC2 linux VMs, following these instructions.
The tools I am using for the building:
apache-maven: 3.2.5;
scala: 2.10.4;
zinc: 0.3.5.3;
Java: jdk1.7.0_79
Linux 32bits
This error message is raised:
Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.0:testCompile (scala-test-compile-first) on project spark-core_2.10: Execution scala-test-compile-first of goal net.alchim31.maven:scala-maven-plugin:3.2.0:testCompile failed. CompileFailed -> [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
The website suggests that the error could be caused by a plugin failure, but provides no details. What is the problem? Is there an approach I can take to resolve the error?
You can use the following pom.xml to build your project
<properties>
<spark.version>2.3.2</spark.version>
<scala.version>2.11.12</scala.version>
<scala.compat.version>2.11</scala.compat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.compat.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.compat.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_${scala.compat.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>package.name.of.main.object</mainClass> <!-- add the path to file containing main method e.g com.company.code.ObjectName -->
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<!-- <phase>compile</phase> -->
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</build>
in the directory containing the pom file run the command: mvn clean install, and your project would be made available as an uber/fat jar in the target directory.
You can then pass the JAR to spark-submit as usual.
Please keep the following points in mind:
Spark and Scala do not support Java 1.7. If you wish to use the
latest Spark 2.x series, you have to use Scala 2.11/2.12 in your
dependencies.
If you are using Spark 1.6, prefer using Scala 2.11, since the support
for Scala 2.10, would not be so readily available.

Conventional workflow to build deployable maven code from repo

I am setting up a Maven repository with the goal of automating much of my build and deploy process. My current workflow begins with bringing up a server, doing a git clone, and running a bunch of mvn install commands to complete the top level application.
To my understanding, Maven is not a tool for deploying applications, and at this point I do not see a need for a continuous integration or continuous deployment server or formal process.
However, thus far I have not been able to find a canonical way to put together the complete, runnable program together with Maven.
This script goes so far as to get the top level jar and install it into the local maven repo:
mvn -DgroupId=me.company -DartifactId=Top-Level-Application -Dversion=1.0-SNAPSHOT -DrepoUrl='http://theserver:8081/nexus/' dependency:get
If I installed everything manually, this application would find all of its dependent jars in a lib/ directory, as Maven ought to do. However this jar as pulled is not deployable - while the dependent jars are installed to a local repo they are not compiled where the classpath expects it.
I imagine if I could rig the above script to pull the pom.xml instead of just the jar and run a mvn install on that, everything would go smoothly (although weirdly that seems to be duplicating the last step of the build process). Another option is for applications to contain all dependent jars rolled up into one giant jar and have no external dependencies.
Which of these (or other) options is the proper way to complete the Maven build process?
Here is our top-level pom narrowed down to as much as I can think would be relevant:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>me.company</groupId>
<artifactId>Top-Level-Application</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Top Level Application</name>
<url>http://company.me</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://host:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>http://host:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>me.company.application.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>me.company</groupId>
<artifactId>First-Library</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- more in-house libraries -->
<!-- third party libraries - Apache Commons, javamail, etc. -->
</dependencies>
</project>
Correct me if I'm wrong -- you want to transform your source code into a compiled JAR, with appropriately-set classpath for its dependencies? That's not what dependency:get is for at all. You should look into the Maven JAR plugin and/or the Maven AppAssembler plugin.

Categories

Resources