I have a project using SwingUtilities2 which is available in Java 5 only. I'm trying to compile it using newer JDK (using Jenkins). When it comes to pure Java only I have no problems, I simply set the compile plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerVersion>${jdk.version}</compilerVersion>
<executable>${jdk.path}/bin/javac</executable>
<fork>true</fork>
</configuration>
</plugin>
Where ${jdk.path} points to Java 1.5 JDK.
When trying to add aspects, it always compiles using default jdk (1.8).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>${jdk.version}</complianceLevel>
</configuration>
</plugin>
I was trying as well setting compilerId + dependencies in maven-compiler-plugin (see: http://maven.apache.org/plugins/maven-compiler-plugin/non-javac-compilers.html), but didn't help either.
Any clues how to do it?
EDIT:
Changed Jenkins job type from Maven to Free Style and invoke it using JDK 1.5. Now compiles with aspects.
Related
I'm running Ubuntu 22.04 right now, and trying to use maven toolchains to compile a Java 17 Spring Boot project while my active jdk is JDK11. I set up my toolchains so they use JDK18 to compile it, and this works great. However, when I try to actually run the tests (with mvn clean install), I get the following error:
...ApplicationTests has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
Everything works out fine when I run mvn clean install -DskipTests. I can see the testCompile goal executing and the test classes being compiled. I'm guessing the issue is that, after compilation with JDK18, maven still tries to use JDK11 to actually run the tests. Any idea how I can tell maven to run the tests with the correct JDK, when I do mvn clean install or similar?
Edit: I managed to get it to work by hardcoding my JDK18 path for the maven surefire plugin. It looks like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<jvm>/usr/lib/jvm/java-18-openjdk-amd64/bin/java</jvm>
</configuration>
</plugin>
While this is alright, I don't like the hardcoding. According to the documentation (seems out of date, but eh), maven-surefire-plugin should be toolchain aware, and should be able to work if I specify my toolchain with something like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<jdkToolchain>
<version>18</version>
<vendor>sun</vendor>
</jdkToolchain>
</configuration>
</plugin>
or not even that, since surefire should be able to derive this from the toolchain I configure with the maven-toolchains-plugin. Relevant toolchain config, just for the record:
<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>18</version>
<vendor>sun</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
in pom.xml <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin> what should be the version no?,i mean from where i will get this version no?
is this the same i.e mvn installation .
See http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source
Although the values are not explicitly defined, it follows the convention of major Java releases - 1.5, 1.6, 1.7, 1.8, etc.
In Maven, I have a codebase that I want to build that needs to target the 1.4 JVM. This is very easy to do in the pom, but I have one problem: The tests for this codebase use 1.5+ constructs.
Is it possible to have Maven compile/run the tests inside a 1.6 JVM, but build the main codebase to target 1.4?
Setting source to 1.6 and target to 1.4 don't work. Maven/Java don't allow this combination.
This is possible, but you need to set the parameters for testCompile rather than compile. You can specify a different target/source combination for the testCompile that you use for the compile.
So for compile you've have a target of 1.4, and testCompile 1.5 or 1.6.
Also, to run the unit tests, you can specify the jvm to use in surefire by using the jvm parameter. This would point to a 1.6 jvm.
It's been awhile since I've done this, but the plugin dependency you want is something like this:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<source>1.3</source>
<target>1.3</target>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</execution>
</executions>
</plugin>
link to source
I was able to achieve what I wanted with this:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<testSource>1.6</testSource>
<testTarget>1.6</testTarget>
<target>1.4</target>
<source>1.4</source>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
I'm attempting to run my Maven build using the usual:
mvn clean install
I'm getting a whole series of errors that read:
annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
How do I use -source 5 when performing the build. My JAVA_HOME is pointing to JDK 1.6.
Add this to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
It is also documented in the Maven FAQ.
For further information take a look at the Compiler Plugin documentation.
it's in the pom.xml. you should have <source>1.3</source> change it to
<source>1.6</source>
I have one jar dependency in my java project that contains sources as well and when I run mvn compile, these java sources appear as class files in my compiled maven output :(...
How can I exclude these files.. (I only want my own compiled files in the compiled output)
I tried something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1-SNAPSHOT</version>
<configuration>
<excludes>
<exclude>**/bv/**/*.java</exclude>
</excludes>
</configuration>
</plugin>
Played with it but they keep appearing in my maven compiled output :( ..
Any idea's ?
My understanding is that 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. So, could you please try this:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<sourcepath>${project.basedir}/src/main/java</sourcepath>
</compilerArguments>
</configuration>
</plugin>
Would the provided scope work?
From: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html:
This is much like compile, but
indicates you expect the JDK or a
container to provide the dependency at
runtime.
You can pass the -implicit:none parameter to the compiler
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-implicit:none</compilerArgument>
</configuration>
</plugin>