Maven compiler plugin vs maven default plugin? - java

Maven is a powerful build tool for Java software projects.
I was reading about Maven plugins. (maven-compiler-plugin)_
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
For any project to run using maven, it definitely need to compile the code.
But i can see many sample projects online which doesn't have this plugin and still work.
Can anybody explain what is the difference in this plugin and by default compiling?

If you do nothing, Maven will call the maven compiler plugin which is called in the maven default lifecycle.
If you want to change the configuration, you need to add the plugin to <plugins> and then change the configuration. Often people do this to change the Java version (which can also be done through properties) or to fix the version of the plugin (instead of always using the latest version).

Related

Use custom SpotBugs plugin in multi-module maven project

I have create a custom SpotBugs plugin for maven like described here in a multi-module maven project. But I cannot figure out how to use it. There really is not much documentation or examples for this.
Like described here I have used pluginList and plugins configuration properties for SpotBugs. But it does not report the bug when I run spotbugs:spotbugs. Other bugs are reported. Any idea what the problem might be?
I am using Spotbugs in maven like this:
<reporting>
<plugins>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>${spotbugs.plugin.version}</version>
<configuration>
<pluginList>custom-spotbugs.jar</pluginList>
<plugins>
<plugin>
<groupId>com.example</groupId>
<artifactId>custom-spotbugs</artifactId>
<version>${custom.build.version}</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</reporting>
Edit: I noticed that I had to run mvn site for the reporting plugins to work. But the next problem is that the configuration values cannot be changed. I had <effort>Max</effort> in the configuration and even if I remove it, it is still used. Are the configuration cached or something?

maven-site plugins 3.3 java.lang.ClassNotFoundException: org.apache.maven.doxia.siterenderer.DocumentContent

Since this night, maven site 3.3 plugins stop to work.
Try to delete local repository, but no change.
Maven 3.3.9
java 1.8
No config or dependencies defined in pom for site plugins
[WARNING] Error injecting: org.apache.maven.report.projectinfo.CiManagementReport
java.lang.NoClassDefFoundError: org/apache/maven/doxia/siterenderer/DocumentContent
I had just started to get this issue also during builds. What worked for me was to specifically define the maven-site-plugin and the maven-project-info-reports-plugin along with the version numbers in the pom.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
This is caused by maven-project-info-reports-plugin updated to 3.0.0, and rely on doxia-site-renderer 1.8 (and have org.apache.maven.doxia.siterenderer.DocumentContent this class), but maven-site-plugin:3.3 rely on doxia-site-renderer:1.4 (and do not have org.apache.maven.doxia.siterenderer.DocumentContent)
We can specific maven-project-info-reports-plugin version in reporting part:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</reporting>
Or we can specify maven-site-plugin to the latest 3.7.1 like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
in build part of pom.
Version of the maven site plugin needs to be explicitly set in the build section too. Here is the example:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
<reportSets>
<reportSet>
<reports>
<report>index</report>
<report>licenses</report>
<report>dependency-info</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
<!-- Part of Maven - specified version explicitly for compatibility
with the maven-project-info-reports-plugin 3.0.0-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</build>
Maven 3 doesn't support Doxia anymore.
Use
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.2</version>
</plugin>
Reference: https://maven.apache.org/plugins/maven-site-plugin/maven-3.html
You really need to add more information (I didn't downvote BTW).
IIRC; if you don't specify a version for a plugin bound to lifecycle phases, you'll get the latest.
Try:
Upgrading to the latest version of maven - 3.5.4 ATOW
Running mvn help:effective-pom and checking which versions are
actually being resolved - if you have an old log from CI or wherever
to compare with..
Explicity setting the maven-site-plugin version
in pluginManagement section
Adding a dependency to maven-site-plugin (see below)
org/apache/maven/doxia/siterenderer/DocumentContent can be found in doxia-site-renderer:
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-site-renderer</artifactId>
<version>1.8.1</version>
</dependency>
I suspect explicitly setting the version of maven-site-plugin to whatever you used to use (incidentally) will work.
Edit: Was chasing a similar issue in maven plugin build testing, explicitly setting maven-site-plugin version (3.7.1 ATOW) in integration pom used by maven-invoker-plugin has worked for me.
The following versions in pom.xml fixed the problem for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
</plugin>
I also hit this error on some of my build jobs today. The fix suggested above, adding a concrete dependency for the maven-site-plugin does work and fixes this issue.
However, what it highlighted for me was the fact I was even running the mvn site goal, which I didn't even know we were running and we don't really require.
My fix was to therefore remove the site goal from my mvn arg, as although the site it creates is actually quite useful, I never knew we were creating it, we never published it anywhere and were actually deleting it every build anyway.
I tried to follow Changhua's advice, and define maven-project-info-reports-plugin to version 3.0.0, and maven-site-plugin to 3.7.1 in my pom file, but found that the maven site still pulled in version 3.3 of the maven-site-plugin, regardless of how I set it.
I finally realized that my problem had to do with our project structure. We have a parent pom, in which we were defining the maven-site-plugin dependency, which was then inherited by the children poms. However, the build pom file was separate, and didn't define maven-site-plugin at all, which allowed maven to pull in the 3.3 version on its own. I added the maven-site-plugin dependency (version 3.7.1) to the build pom file, so that it now exists in both the build pom file and the parent pom file, and now the build is correctly using version 3.7.1, and is passing again.

Eclipse Kepler is unable to set the Java jdk 1.8 when maven project is updated

Installed java version is 1.8, while selecting this version in pom.xml and updating the maven project,it automatically jumps from 1.8 to 1.4, due to which I am unable to have Lambda expression specific code.
I am using eclipse kepler.
Any idea what is happening here ?
Add something similar to this to your pom. You might have to tweak the plug in version.
<project>
[...]
<build>
[...]
<plugins>
<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>
</plugins>
[...]
</build>
[...]
</project>
Apologies I'm on mobile and the editor sucks for code formatting.
Edit: As noted in the accepted answer you also need to enable Java 8 support in Kepler.
With the help of Hogler's comment above I am able to reswolve this issue
Just did this :
https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

Incremental java compile with maven (like Eclipse does)

I want to use maven to build projects in which there are unresolved compilation problems.
The main purpose is package and deploy or run aplications using some kind of stubs for classes that contains compilation errors, like I understand that Eclipse does (thanks to JDT Core).
I configurate maven java compiler plugin following Apache Maven documentation at Using Non-Javac compiler to use Eclipse compiler. Thinking that maybe should set some arguments to modify the compiler/builder behaivor I was reading Help Eclipse - Compiling Java code but I don't realize which compiler/builder option or combination of these does the trick.
So far, the next configuration of the maven java compiler plugins compile using the eclipse compiler and package the application including generated .class (jvm bytecode) only for java classes without compilation errors. To get this behaivor it just require use the eclipse compiler (see compilerId and the dependency) and set failOnError=false.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>eclipse</compilerId>
<source>1.7</source>
<target>1.7</target>
<optimize>true</optimize>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<failOnError>false</failOnError>
<compilerArguments>
<org.eclipse.jdt.core.compiler.problem.fatalOptionalError>disabled</org.eclipse.jdt.core.compiler.problem.fatalOptionalError>
<org.eclipse.jdt.core.compiler.problem.forbiddenReference>ignore</org.eclipse.jdt.core.compiler.problem.forbiddenReference>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</plugin>
With this configuration I could run java application as long as the execution doesn't use classes not included for compilation errors (because the stubs aren't generated) but on a Java EE container, the classloading will faild so the application can never be deployed.
I appreciate any help on this.
Just for share the solution, at that moment I just replace the plexus-compiler-eclipse with tycho-compiler-jdt to get the desire behaivor.
The proceedOnError parameter indicates that it must keep compiling in spite of errors, dumping class files with problem methods or problem types how to deal with the compilation errors.
Next is the final configuration sample.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>jdt</compilerId>
<source>1.7</source>
<target>1.7</target>
<optimize>true</optimize>
<failOnError>false</failOnError>
<compilerArguments>
<proceedOnError/>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-jdt</artifactId>
<version>0.22.0</version>
</dependency>
</dependencies>
</plugin>
There is more plugin configuration examples in the Tycho FAQ.
And the possible compiler arguments are described in section Using the batch compiler of the Java development user guide (Eclipse Help site).

How can I switch the bytecode target level in IntelliJ using Maven 3 profiles

I am using maven profiles to switch between two "setups" in Intelli J. How can I switch the bytecode target level? That is: I want to change the following setting in compiler.xml
<bytecodeTargetLevel>
<module name="finmath-lib" target="1.6" />
</bytecodeTargetLevel>
Note: I tried to perform this via the following part in the respective Maven 3 profile
<profile>
<id>java-8</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
but this does not work!
Note: The pom.xml etc. belongs to the finmath lib project, and if you are interested, it can be found at www.finmath.net
I am using IntelliJ IDEA 14.1.3 and it works with following config ...
<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>
Hope this is helpful. Thanks!
The problem is that you want to change it to 1.8.
I've tried to switch IntelliJ's compiler version between 1.7 and 1.6 or 1.5 using Maven profile successfully.
However when you want to change it to 1.8, the config will be ignored.
I don't know whether this is maven's problem or Intellj's.
It seems that the JDK 1.8 is not well supported now, which is understandable.
You can open the Maven Projects tool window in IntelliJ and select the profile you want to use from the list.
In IntelliJ 13, File > Project Structure, set the Project language level to 8.0.

Categories

Resources