Eclipse maven build path issue in case of custom sourceDirectory - java

I have converted an existing Java project to a Maven project and Maven builds everything perfectly when using the command line.
When I import the same project into Eclipse and compile (by right-clicking the project -> runs as Maven build, it still compiles without any issue.
However, I am not able to see the source folder. When I check the build path it gives warning - build path entry is missing.
I am not using standard src/main/java since I had a pre-existing folder structure for the project which could not be changed.
Here's my pom (notice the sourceDirectory tag):
<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>com.mycom</groupId>
<artifactId>maven</artifactId>
<version>17.4</version>
<name>maven</name>
<properties>
<projecrt.rootDir>../../java</projecrt.rootDir>
</properties>
<build>
<finalName>re</finalName>
<sourceDirectory>${projecrt.rootDir}</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Build-Version>${buildversion}</Build-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
and here's my directory structure:
Src/java
----maven/pom.xml
----com/mycom/<...> // application code

You can use this build-helper-maven-plugin
And configure it in your pom file this way
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>some directory</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
and now when you do mvn eclipse:eclipse the custom source folders will get added to the classpath entries in the .project file.
So, you wont need to do any manual configuration, your configuration will simply get build at run-time.
You can do the same for your test sources as well, if you have your tests in a custom source folder.
Hope this helps.

Once your Java project is imported in the Eclipse workspace, you can specify the actual src folder:
Click Open Java perspective Window > Open Perspective > Other... > Java to change to the Java perspective.
In Project layout group, change selection to Create separate source and output folders and edit Configure default... to modify Source folder name from "src" to "sources".
In the settings of your project (project => property => java build path), you can also change/add src folders:

Related

How to set a build path from terminal

In Eclipse, I can right click on the project and then click on Build Path and Use as a source folder. How can I do this from terminal?
For context, I have a maven project in Eclipse which I can run properly if I do the above mentioned things. I want to do the same thing from terminal. What is the command to do this from terminal?
Please follow the below steps If you prefer to add some external folder as source folder through the terminal way. Eclipse treats the following folders as source folders by default in case of maven project:
src/main/java
src/main/resources
src/test/java
src/test/resources
This case applicable even when you are run the maven project through the command line interface from the project root. If you like to add any other folder as source apart from the above list, which can be easily achieved through the build-helper-maven-plugin. Just create the required source folder through the terminal and then add the folder entry in the below maven plugin.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>**some source directory you prefer**</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
for more about the plugin

Creating Windows EXE from Java(FX) through Eclipse [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 4 years ago.
I can't create Windows software through Eclipse. Using "Run" does run the app as it should, but I would like to start it without Eclipse, preferably as an independent Windows app, using a mere double-click.
Although this suggests several approaches, they all require additional knowledge that I do not posses.
I have tried using Launch4J but it also requires additional knowledge. I am astounded that it is so difficult to create a simple Windows-Java-app.
EDIT
I am using this pom.xml:
<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>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<name>baz</name>
<description></description>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<mainClass>application.Main</mainClass>
<updateExistingJar>true</updateExistingJar>
<skipCopyingDependencies>true</skipCopyingDependencies>
<useLibFolderContentForManifestClasspath>true</useLibFolderContentForManifestClasspath>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
But Eclipse gives this error message:
Failed to execute goal com.zenjava:javafx-maven-plugin:8.8.3:build-jar
the goal being
clean compile package -e
Maven is installed, trying to run it with minimal appendancies.
Launch4j I know it can make an exe Wrapper over the Java, and it is cross platform.
Check here:
http://launch4j.sourceforge.net/
First of all, you can't create "Windows-Java-App". Java applications run on JVM (Java Virtual Machine) which means you wont be getting an exe file. What you need is a JAR file. For info on how to create a JAR file in Eclipse, see here:
https://stackoverflow.com/a/21110972/4440179
For JavaFX follow this guide: https://wiki.eclipse.org/Efxclipse/Tutorials/Tutorial1
I recommend making a Maven project to help you build an executable JavaFX Jar.
Firstly you'll need to make sure that Eclipse has the Maven plugin Help -> About Eclipse IDE and make sure that Eclipse.org - m2e is installed.
Then make a Maven project New -> Other.. -> Maven Project, fill in the basic stuff to create the project then put your source files under src/main/java of this new project.
There should be a pom.xml file in the root of this new project; This file helps manage your project dependencies and build process.
To make easily build an executeable add the below plugins to your pom.xml file to modify the build process.
Then you can just right click your pom.xml then Run As -> Maven Build ... then put clean compile package -e as your goals, refresh your MyProject/target directory and find your Jar under MyProject/target/javafx/app/jarname.jar
Also, if you want, you can look into using the launch4j maven plugin to have maven build the native executable for you, else your Jar will require the user have Java installed but it will still be a double-clickable JavaFX Jar.
<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>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<name>baz</name>
<description></description>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<mainClass>path.to.my.Main</mainClass>
<updateExistingJar>true</updateExistingJar>
<skipCopyingDependencies>true</skipCopyingDependencies>
<useLibFolderContentForManifestClasspath>true</useLibFolderContentForManifestClasspath>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Intellij Java 2016 & Maven : how to embed dependencies in JAR?

I'm using Intellij Java 2016.2.2 and Maven to create a very simple Java console application.
I want to add an external library, so I add my dependency in Maven like this:
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>2.12</version>
</dependency>
It works fine when I run it in the IDE, but not in an external console (I have the following error: java.lang.NoClassDefFoundError).
I checked and for some reason, the external JAR is not added in the JAR I just generated. I also tried many things in "File -> Project Structure", but still not working...
I just want to build my JAR with my dependencies in it, so I can simply run my application in a console using:
java -jar myproject.jar
How can I do that? Thanks for your help!
I finally managed to generate this JAR with Intellij Java, here is how I do:
add the dependencies in the pom.xml file
go to File -> Project Structure -> Artifacts -> New -> JAR -> From module with dependencies
choose the Main class and click OK
in your project, in src/main, create the "resources" folder
move the "META-INF" (with MANIFEST.MF in it) folder in this "resources" folder
go to Build -> build artifacts to build the JAR
EDIT
A better (and easier way) to do it is adding the following lines in the pom.xml file :
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>your.MainClass</mainClass>
</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>
</plugins>
</build>
then use the "clean" and "package" maven commands.
The last 3 steps above (about MANIFEST.MF) still seem to be mandatory.
Okay, so you basically want to create a "fat jar" (sometimes called assembly), that contains all its own dependencies (usually, the dependencies are external).
You need to use a Maven plugin for that. Below is a sample assembly plugin configuration jar-with-dependencies:
<project>
...
<build>
...
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
...
</project>
then, simply run
mvn package

How to add an extra source directory for maven to compile and include in the build jar?

In addition to the src/main/java, I am adding a src/bootstrap directory that I want to include in my build process, in other words, I want maven to compile and include the sources there in my build. How!?
You can use the Build Helper Plugin, e.g:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>some directory</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
NOTE: This solution will just move the java source files to the target/classes directory and will not compile the sources.
Update the pom.xml as -
<project>
....
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
...
</build>
...
</project>
http://maven.apache.org/guides/mini/guide-using-one-source-directory.html
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
also see
Maven compile with multiple src directories
With recent Maven versions (3) and recent version of the maven compiler plugin (3.7.0), I notice that adding a source folder with the build-helper-maven-plugin is not required if the folder that contains the source code to add in the build is located in the target folder or a subfolder of it.
It seems that the compiler maven plugin compiles any java source code located inside this folder whatever the directory that contains them.
For example having some (generated or no) source code in target/a, target/generated-source/foo will be compiled and added in the outputDirectory : target/classes.
To mark a folder as generated sources, AND have it picked up by IntelliJ, use the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>
</configuration>
</plugin>
I spent an hour searching on how to avoid IntelliJ reverting after I manually marked target/generated-sources as a generated sources folder. The codehaus.mojo plugin didn't work. But this solution did!
You can add the directories for your build process like:
...
<resources>
<resource>
<directory>src/bootstrap</directory>
</resource>
</resources>
...
The src/main/java is the default path which is not needed to be mentioned in the pom.xml

maven build with war and jar pushing odd artifacts to internal repo

I have a maven project where I am building a war file, but I am also using the maven-jar-plugin to build a jar in the same project.
--DISCLAIMER--
I know this is not the 'correct' way to do this, but there are some other issues occurring when splitting this into a jar project and a separate war project with some 3rd party plugins.
I am seeing some strange behavior with this. Below is my project structure.
warproject
-src
--main
---webapp
----WEB-INF
-----web.xml
---java
----com.test.myclass
-----test.java
-pom.xml
When I build this project, i get the correct war and jar file in my target directory, however in my local .m2 repo something strange happens. The war file that is installed is named correctly war-jar-0.0.1-SNAPSHOT.war, however the contents of this file are the contents of my jar file. This also occurs if I do the inverse. i.e. if I setup my project to build a jar and use the maven-war-plugin to build the war, the archives in my target directory are correct, but my local repo has jar file with the contents of my war file. Below is the pom file I am using.
<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>com.test</groupId>
<artifactId>war-jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<jarName>${project.artifactId}-${project.version}-client</jarName>
</configuration>
<executions>
<execution>
<id>make-a-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
The console output for this is the following, it shows that the jar is being uploaded as the war.
Installing /home/me/work/src/war-jar/target/war-jar-0.0.1-SNAPSHOT.jar to /home/me/.m2/repository/com/test/war-jar/0.0.1-SNAPSHOT/war-jar-0.0.1-SNAPSHOT.war
--UPDATE
I got this working, but I had to change the phase of my 'make-a-jar' execution to install from package. This works fine and the correct artifacts are uploaded, but I am still confused as to why this makes a difference. Obviously the artifact is generated at a different lifecycle phase, and hence is not around at the time of the original install for the project, hence the wrong file is not uploaded. This seems like a 'hack' and I would like to understand why this is behaving this way.
I'm answering my own questions since I didn't get any information that helped me get to my solution. See my update on my original question for my solution.
This also works,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<!--
When also running maven-jar-plugin correct maven-war-plugin's ID
from "default-war" to "default-jar"
-->
<id>default-jar</id>
<phase>package</phase>
<goals><goal>war</goal></goals>
<configuration>
...
</configuration>
</execution>
</executions>
</plugin>
Refer to http://maven.apache.org/guides/mini/guide-default-execution-ids.html
To figure out why your project behaves as is, analyze the Effective POM.
You need to specify the configurations for the maven-install-plugin to achieve this. Add the following plugin config under <build>.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>

Categories