I want to use Maven to execute a certain plug-in that only needs the source code but I do not want Maven to compile anything (mostly because the project just doesn't compile).
How do I tell Maven to skip the compile step and just launch its plug-in and then package the generated resources together in a nice JAR? (The procedure of the last step is already known to me.)
Additional Info:
So we tried a lot of things right now, e.g.:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<excludes>
<exclude>**/*</exclude>
</excludes>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
Though when we do a mvn package we get this:
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) # project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling ALOTOF source files to /home/myname/dir/dir/project/target/classes
message edited ofc.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<skipMain>true</skipMain> <--Skip
</configuration>
</execution>
</executions>
</plugin>
Set this to 'true' to bypass compilation of main sources. Its use is
NOT RECOMMENDED, but quite convenient on occasion. User property is:
maven.main.skip.
mvn package -Dmaven.main.skip
Maven functionality is partly organized in plugins, that contains goals. These goals can be executed without being part of a lifecycle. Eg for for the jar-plugin's jar-goal you would invoke:
mvn jar:jar
If you browse through the list of available plugins you will probably find the functionality you are looking for. If it is necessary you could even define an "assembly" to select the files you want to bundle into an archive.
To prevent Maven compilation by default, first make sure that the configuration of maven-compiler-plugin has useIncrementalCompilation = false:
<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>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
Also, in case your Maven Profile uses maven-clean-plugin, then by default it discovers and deletes the directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, and project.reporting.outputDirectory.
To disable these default cleanups, add to maven-clean-plugin configuration: excludeDefaultDirectories = true:
<excludeDefaultDirectories>true</excludeDefaultDirectories>
so I used it like this and it worked. ( false)
<executions>
<execution>
<id>default-compile</id>
<configuration>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<failOnError>false</failOnError>
<compilerArgument>-Xlint:unchecked</compilerArgument>
<compilerArguments>
<source>${maven.compiler.target}</source>
<target>${maven.compiler.source}</target>
</compilerArguments>
</configuration>
</execution>
I would define a separate pom just for building that separate artifact.
In that pom, you only use your resources generation plugin and the packaging ones.
Then you don't have to worry about it doing too much.
Related
I had to integrate some legacy code into my maven build, so I used the maven-recommended toolchains plugin to change the java version:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.5</version>
</jdk>
</toolchains>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
<configuration>
<compilerArgs>
<arg>-Xmaxerrs</arg>
<arg>1000</arg>
</compilerArgs>
</configuration>
</plugin>
Then I ran into the max 100 compile errors problem which required passing special options to javac and found I was able to do it just with maven compiler:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
<configuration>
<fork>true</fork>
<compilerVersion>1.5</compilerVersion>
<executable>C:\Java\jdk-1.5.0_22\bin\javac.exe</executable>
<source>1.5</source>
<target>1.5</target>
<compilerArgs>
<arg>-Xmaxerrs</arg>
<arg>1000</arg>
</compilerArgs>
</configuration>
</plugin>
Both snippets produce the same result: the java compiler is changed from the maven default to java 1.5. Both run in about the same amount of time, so there's no visible performance difference. I'd like to know if there are any benefits of one over the other so I know when to use each.
They do different things:
The compiler plugin specifically configures how your Java code is compiled (and only that).
The toolchains plugin just ensures that other plugins are all using the same Java tool chain (i.e. the same JDK) to compile, run, test, generate javadocs and so on.
This is explained in the respective plugins' documentation.
Note that not all plugins are "tool chain aware", but the compiler plugin is.
... are any benefits of one over the other
Well there there are things you can do with one and not the other and vice versa. For example, you can't set Java compiler options using the toolchain plugin.
However, they are not mutually exclusive. You can use both in the same POM file.
I have some test related classes that I want to exclude from the compiled jar output for a project. This is a legacy project and I don't want to lose existing revision history by moving the classes to src/test. Since I am already using build-helper-maven-plugin I thought I would be able to specify an exclusion pattern there, but so far nothing I have tried seems to work. I run
mvn clean install package
in my project root and I see the log message
[INFO] --- build-helper-maven-plugin:3.0.0:add-source (add-source) #
Person-ejb --- [INFO] Source directory:
/media/psf/Home/Documents/workspace/optics/optics/Person/ejb/src
added.
but when I look at the compiled jar it still containts the test directory and its contents. Any idea what I could be doing wrong?
My pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/</source>
</sources>
<excludes>
<exclude>**/test/**</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>add-reource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>resources/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Context
I believe this is a bug in build-helper-maven-plugin or possibly a misprint in the usage documentation. The <excludes> tag is completely ignored by the plugin.
It does not even appear in the code completion in Eclipse
Solution
While you can't exclude files in the builder-helper-maven-plugin, you can exclude files in the maven-compiler-plugin. So if you simply add the following configuration to your maven-compiler-plugin it should exclude the **/test/** directories
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<excludes>
<exclude>**/test/**</exclude>
</excludes>
</configuration>
</plugin>
Best Practice
I feel obligated to remind the reader that this is an anti-pattern in maven. Ideally you would create separate pom files for each source directory, then add them as separate modules in your parent pom file. I realize this may be a larger refactoring effort though
An existing maven pom project <packaging>pom</packaging> which currently collects and packages resources needs to be extended to validate some of the resources.
In the same project I created a java-source directory src/main/java and in there I created a small java class to validate some of the resources. In addition I configured the maven-compiler and exec-maven plugin in the pom.
The java class runs fine in the IDE but it fails when I do mvn clean install it fails because it cant find the compiled class file. This is because the compile/test-compile phase is not available for pom-packaged projects.
My questions are:
Can I modify the compiler plugin to execute (compile) in a different phase than the default compile-phase. (I tried with adding an execution tag but no success)
Why is the exec-maven plugin executed because this was defined in test phase, which according to the docs is not part of the pom-package.
Are there other possibilities to run this validation task in the pom?
Modifying the packaging from pom to jar is a political sub-optimal solution.
Yes, you can configure maven-compiler-plugin to run the compilation in the package phase of the pom packaging.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<mainClass>com.example.validate.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Can you tell me how to call maven surefire in command line with the following configuration ?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>Custom tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<classesDirectory>target/generated/classes/normalCase/</classesDirectory>
<reportsDirectory>target/generated/reports/normalCase/</reportsDirectory>
</configuration>
</execution>
</executions>
</plugin>
When surefire is defined like this in my pom.xml and I execute the phase test, it work exactly how i want it to work : it tries to run my tests on the classes located in target/generated/classes/normalCase.
So I tried this command line:
mvn surefire:test -DclassesDirectory="target/generated/classes/normalCase/"
But no, it keep checking the classes in the default value directory which is "target/classes".
So how can I achieve this in command line ?
To recap the situation you have. You are generating multiple version of your source code during the build, each of those version ends up in a separate folder under target. For each of those versions, you would like to execute your unit tests with the maven-surefire-plugin. Let's consider the base directory to be target/generated/classes. That means you have multiple subdirectories target/generated/classes/version1, target/generated/classes/version2... for each version.
A possible solution would be to use the iterator-maven-plugin to iterate over all subdirectories of a folder and invoke the maven-surefire-plugin from all those subdirectories. The variable #item# holds the current item.
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.3</version>
<executions>
<execution>
<id>iterate</id>
<phase>test</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<folder>target/generated/classes</folder>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<goal>test</goal>
<configuration>
<classesDirectory>target/generated/classes/#item#</classesDirectory>
<reportsDirectory>target/generated/reports/#item#</reportsDirectory>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
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>