Question/Problem:
How to add an additional source folder to a standard java console Maven project using Eclipse (Luna) so that Maven sees the path for jar build.
The expected result is to somehow configure pom.xml so that Maven plugins in Eclipse can be executed cleanly.
Assumptions - a successful add of an additional source folder via project (right click) -> new -> source folder.
To let Maven know about the new source folder for building a jar I had to add the following to my pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<includes>
<include>[your source folder goes here]/**/*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>[your source folder goes here]</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Be sure to add the pluginManagement tags around plugins as omitting this tag prevented the mojo plugin to recognize the executions tag.
Perhaps more later on the success of the actual jar construction...
Add generated sources into configuration of maven-compiler-plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<generatedSourcesDirectory>[additional directory]</generatedSourcesDirectory>
</configuration>
</plugin>
or provide additional execution:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile-additional-sources</id>
<goals><goal>compile</goal></goals>
<configuration>
<source>[additional sources]</source>
</configuration>
</execution>
</executions>
</plugin>
Related
Can anyone please tell me how to apply the semver to the java maven project? I tried many ways, but I didn't find any useful resources to automatically increase the version when I push the code to the branch. I'm using Github action workflow to deploy the project into GitHub.
Thank you.
My first approach is to use the command line but you have to configuration the following in your pom file before. You can of course directly use the command line and put everything on the plain command without this setup but it's very inconvenient
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.9.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<executions>
<execution>
<id>major</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.nextMajorVersion}.0.0-SNAPSHOT</newVersion>
</configuration>
</execution>
<execution>
<id>minor</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.majorVersion}.${parsedVersion.nextMinorVersion}.0-SNAPSHOT</newVersion>
</configuration>
</execution>
<execution>
<id>patch</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT</newVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>parse-version</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
By using the above configuration you can change/update the version of your project like this:
mvn build-helper:parse-version versions:set#major
This will increment the major version and set minor and patch version to 0.
mvn build-helper:parse-version versions:set#minor
This will increment the minor version and set patch version to zero.
mvn build-helper:parse-version versions:set#patch
this will increment the patch version. Afterwards you have to commit your changed back into your version control system (for example git).
I recommend to define this kind of setup into a parent pom and reuse it for multiple projects. A detail explanation why and how this works can be found here https://blog.soebes.de/blog/2021/04/05/maven-plugin-configuration/
Using the maven-release-plugin is also an option. It will make also the tags in your version control.
I have a custom Maven plugin which makes use of JDK 12 preview features. I compile the plugin setting --enable-preview as compiler arg, i.e.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<compilerArg>--enable-preview</compilerArg>
</compilerArgs>
</configuration>
</plugin>
When I want to execute the plugin, I add the plugin like this in the POM:
<plugin>
<groupId>my.group</groupId>
<artifactId>my-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>my-goal</goal>
</goals>
</execution>
</executions>
</plugin>
But this fails with:
Preview features are not enabled for MyPluginMojo. Try running with '--enable-preview'
How can I enable preview features in a plugin execution?
For me, I had to add a config file to my build directory at:
.mvn/jvm.config
containing:
--enable-preview
This will make sure that Maven passes the correct parameters to JVM
You made a mistake in your pom. <compilerArgs> takes nested <arg>, like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
For JDK 17, this works for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
I cannot stop my javafx-maven-plugin to insert the version number into the jars file name. I tried to set finalName to "${pom.artifactId}" but that doesn't help.
What can I do?
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.1.4</version>
<configuration>
<mainClass>application.Main</mainClass>
<jfxAppOutputDir>${project.build.directory}</jfxAppOutputDir>
<finalName>${pom.artifactId}</finalName>
</configuration>
<executions>
<execution>
<id>create-jfxjar</id>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
It is an old question, but:
1.Insert final jar Name in this way:
<build>
<finalName>your.customname.without.versionNumber</finalName>
2.Edit javafx-maven-plugin configuration, add this inside the configuration tag:
<jfxMainAppJarName>${project.build.finalName}.jar</jfxMainAppJarName>
The complete configuration should looks like this:
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<jfxMainAppJarName>${project.build.finalName}.jar</jfxMainAppJarName>
<mainClass>com.my.amazingapp.MainClassName</mainClass>
<vendor>The Tiger Programmer</vendor>
<manifestAttributes>
<developer>Me</developer>
<contact>me#company.com</contact>
</manifestAttributes>
</configuration>
</plugin>
Now if your finalName is "MyAmazingProject", after you run mvn clean jfx:jar you will find a file MyAmazingProject.jar under
/target/jfx/app/MyAmazingProject.jar
This jar is a executable jar with Manifest Info.
I had old web application project. Then I added pom.xml, and add maven-war-plugin. In the old project sources were in "Java Resources/src"directory.
In my maven-war plugin I trying overriding default source directorie like this, but not working.
during compilation i see:
`
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # webproj ---
[INFO] No sources to compile
[INFO]
`
my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
<webappDirectory>WebContent</webappDirectory>
<source>${project.basedir}\src</source>
<target>${maven.compiler.target}</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
If you are planning to migrate to Maven, I suggest that you follow the Maven Standard Directory Layout. Consequently, you should move your source files to src/main/java, static resources like .properties files to the src/main/resources, and webapp resources like CSS and JavaScript files to src/main/webapp.
The Maven-war-plugin use the project source folder, to override the location you must put in your pom like this:
<build>
<sourceDirectory>/Java Resources/src</sourceDirectory>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${version.build-helper-maven-plugin}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>social/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
...
</plugins>
<build>
You can specify warSourceDirectory in maven-war-plugin configuration:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>old/project/source</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
I have configured cobertura in a maven 3 project. My requirement is to configure cobertura in parent pom and configure the ignore, exclude values in child poms in multiple projects. The basic configuration of cobutura plugin in pom xml with reference to it in child pom worked fine. But when i tried to configure ignore, exclude packages in child pom and added instrument goal in parent pom, i'm getting directory issues with the plugin. Google search gave me this link, but not successful so far. : http://blog.bielu.com/2012/01/errata-to-maven2-maven3-migration.html
Here is my configuration:
Parent pom:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<check>
<haltOnFailure>true</haltOnFailure>
<branchRate>50</branchRate>
<lineRate>50</lineRate>
<haltOnFailure>true</haltOnFailure>
<totalBranchRate>50</totalBranchRate>
<totalLineRate>50</totalLineRate>
<packageLineRate>50</packageLineRate>
<packageBranchRate>50</packageBranchRate>
</check>
</configuration>
<executions>
<execution>
<id>instrument-code</id>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Child pom:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<instrumentation>
<ignores>
<ignore>to.ignore.*</ignore>
</ignores>
<excludes>
<exclude>to/exclude/*</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
</plugins>
</build>
Error thrown:
[ERROR] Failed to execute goal org.codehaus.mojo:cobertura-maven-plugin:2.5.1:instrument (instrument-code) on project test: Unable to prepare instrumentation directory. source and destination are the same directory.
Tried several configuration, but no luck yet. Please advice me on this.
Thanks.