maven prevent denpendency compile - maven

I have a custom jar which including java sources; Maven tries to compile when it builds. How do I skip source compile in the jar file? I have tried such as exclude with some pattern in the compiler-plug in and source directory define but I have not get any luck. Thanks!
C05

This is a normal behavior of javac that searches the whole classpath for source files to compile unless the -sourcepath option is given (and this would be the solution here).
Unfortunately, there is a Jira issue about -sourcepath not being passed to javac by the Maven Compiler Plugin (see MCOMPILER-98). But there is a workaround:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<sourcepath>${project.basedir}/src/main/java</sourcepath>
</compilerArguments>
</configuration>
</plugin>

Related

Maven Compiler plugin -source is different than -target parameter

I have the following in the mail pom.xml file:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.7</target>
</configuration>
Is it correct? Please tell me whether what are the actual usages of -source and -target tags?
Also, my requirement says "For the maven plugin, just make sure is it compiled in Java 7".
source and target parameters are propagated from maven compiler plugin to javac compiler from SDK.
Having source less than target is questionable. As #khmarbaise mentioned in comment, this means your source code is compiled JDK6 but you require JVM for Java 1.7.
However it makes sense to have target lesser than source. For example
<source>1.8</source>
<target>1.6</target>
should compile JDK 8 code and produce bytecode compatible with Java 1.6+ JVMs. Although I have never tried it - behaviour may even differentiate between different Java language implementations (OpenJDK, OracleJDK,...)

Confusion on maven compiler plugin

I am quite confused about maven compiler plugin and what it does. I have a project that has several modules. In my top pom.xml I have a section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
My understanding is this specifies the JDK compiler version used to compile the code, and this section get inherited by all the modules. What I don't get is in my IntelliJ IDEA I can still specify project JDK in the settings and it seems like that setting overrides this. When I run maven install in the IDE I can confirm that it is using javac from JDK 8 to compile. So what does this section do exactly?
You are correct; maven-compiler settings should be inherited by child modules.
I don't know about IntelliJ, but I can tell you that Eclipse picks&chooses whatever it wants from maven config, and for all the rest it uses its own settings.
Therefore, I'd expect IntelliJ may do something similar?
The simplest way to test this is to run a mvn clean install via command line, and see which "wins". If you get artifact compiled with 1.8 then it means you're missing something in Maven config which causes those settings not to propagate to children. If you get artifact compiled with 1.7 then it is IntelliJ who does it and not maven-compiler-plugin.

Does maven-release-plugin also do what maven-compiler-plugin does?

In project pom.xml is it required to include both maven-release-plugin and maven-compiler-plugin? The reason I want the release plugin is because I want to release the project at the end of a sprint/release. I understand that the compiler plugin will compile the source code, however I am not sure if the release plugin will also compile the source code, apart from updating the pom and checking out/in to scm.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
Any help with this is much appreciated.
If unspecified, Maven will know what to do when it needs to compile the code. However, in order to be able to control things like source and target versions, you will need to have a declaration of the maven-compiler-plugin.
It is generally good practice, to explicitly define the plugins you are using, along with their configurations and above all their versions, so that you have a guarantee of what was working.
What the Maven Release Plugin does is described here.
Your question is about the bullet "Run the project tests against the modified POMs to confirm everything is in working order". There is not that much magic here. What happens is that the maven-release-plugin will start a new Maven session. It will execute "mvn verify" in the same folder where you ran "mvn release:prepare".
All the steps done by the maven-release-plugin could also be done by hand, but that's asking for mistakes.

Maven Compiler Plugin Fork Option Not Working

I have a maven pom file with the following compiler-plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<executable>${path_to_JDK6}</executable>
<fork>true</fork>
<compilerVerison>1.6</compilerVerison>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
My system's JAVA_HOME is set to a 1.5 JDK. When I run mvn package, maven throws the following error message:
Failure executing javac, but could not parse the error:
javac: invalid target release: 1.6
Usage: javac <options> <source files>
Does anyone have any ideas about why Maven is using the 1.5 JDK instead of forking to the 1.6 executable? Is there any debugging option that I could use? Are the fork and executable options broken in Maven 2?
Note: My system admin would not allow me to change the value of JAVA_HOME and some of my new libraries are written for java 6. So I would like to find a workaround to make maven compile with JDK 1.6.
My ${path_to_JDK6} variable only included the path to the folder containing the JDK. It did not include the /bin/javac. When I added /bin/javac, the 1.6 compiler was invoked.

maven can't add files in generated-sources for compilation phase

I use Apache Thrift to generate code in target/generated-sources.
The Thrift compiler produces a directory named gen-java which contains all the Java code. When I execute mvn compile, the code is generated correctly in target/generated-source/gen-java, but in compilation phase, it complains can't find the classes which defined in gen-java.
In my understanding, Maven 2 automatically adds generated sources, is that right?
And what if my testing code also depends on the generated-sources, do I have to manually specified the compiler includes?
In my understanding, maven 2 automatically add generated sources, is that right?
Nothing automatic, plugins generating source code typically handle that by adding their output directory (something like target/generated-sources/<tool> by convention) as source directory to the POM so that it will be included later during the compile phase.
Some less well implemented plugins don't do that for you and you have to add the directory yourself, for example using the Build Helper Maven Plugin.
And since you didn't provide any POM snippet, any link, I can't say anything more.
And what if my testing code also depends on the generated-sources, do I have to manually specified the compiler includes?
As I said, generated sources are usually added as source directory and compiled and are thus available on the test classpath without you having to do anything.
Generated sources are not compiled or packaged automatically. Some IDEs (i.e. IntelliJ) will however show them as source folders.
To make generated sources visible to maven add a add-source-step to the build/plugins node of your pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/gen-java</source><!-- adjust folder name to your needs -->
</sources>
</configuration>
</execution>
</executions>
</plugin>

Resources