Maven webapp "javac: -endorseddirs requires an argument" - java

I have a Maven webapp that uses the maven-compiler-plugin . A few days ago I could compile and run the app just fine, however something happened and compilation now fails with the following error:
[ERROR] Failure executing javac, but could not parse the error:
javac: -endorseddirs requires an argument
Usage: javac <options> <source files>
It has something to do with the compiler but I can't understand it. Here's my pom.xml (just the plugins):
<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>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
</executions>
</plugin>
</plugins>
I have tried some solutions like this.

As far as I know, ${endorsed.dir} is not a standard Maven property. Have you copied this example from somewhere without replacing ${endorsed.dir} with an actual value? Or did you have this value defined elsewhere in your pom.xmlbut it has been removed?
If this is the case, Maven would treat the field as blank and I can imagine the compiler would receive no argument for the -endorseddirs parameter.

Java compiler is not able to locate property ${endorsed.dir} in you POM, Make sure it is defined in you POM. To check configuration of POM by mvn help:effective-pom, That will show actual POM after factoring all configuration.

Related

What is the difference between maven compiler plugin and maven toolchains plugin?

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.

Why to specify java version in maven build?

When I was building my project like :-
mvn clean install -DskipTests
, then it was giving some error.
After that, I just added, -Djdk.version=1.8, then it works fine.
Can someone tell what is the reason for this?
Try something like this in your pom:
<build>
<pluginManagement>
<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>
</pluginManagement>
....
</build>
It may be because you might be missing the maven-compiler-plugin.
Try the following:
<build>
<pluginManagement>
<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>
</pluginManagement>
....
</build>
From Maven 3, it defaults to JDK 1.5. So if you do not include the version, it will take JDK 1.5 as default compiler version.
Since, you mentioned that you used JDK 1.8, and the error disappeared. So if you had maven-compiler-version defined in the pom.xml, the error might be because the version would had not been defined.
So by default, it pointed to JDK 1.5 and it was trying to compile the code which would be defined for JDK 1.8 and not for JDK 1.5.
So, it is better to define the correct java version in the pom.xml
Why to specify java version in maven build?
Because Maven won't try to guess the Java version your project was created with (the one you probably only configured in your IDE).
You need to specify the earliest version supported in your pom.xml with:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

Endorsed directory is missing

In eclipse I receive this error: "Endorsed directory ... is missing. You may need to perform a Maven command line build to create it."
It seems to be related to this plugin in my pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${project.build.directory}/endorsed</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
The project builds successfully on the command line. I did a clean build in Eclipse but still have this error.
What is wrong with my setup?
I think the problem is with
<endorseddirs>${project.build.directory}/endorsed</endorseddirs>
Refer to Link - Pass Compiler Arguments

How to create a new classes for each compilation using maven plugin

In case of my project I need to create new classes after each compilation. For compilation I'm using maven compiler plugin 3.1. I tried to use compilerReuseStrategy = alwaysNew option but it didn't make any affect, it always compile only changed classes. Here is plugin declaration in pom.xml:
<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>
<compilerReuseStrategy>alwaysNew</compilerReuseStrategy>
</configuration>
</plugin>
An I doing something wrong or that's a bug and this option really doesn't work?
If you are talking about the incremental feature fo the maven-compiler-plugin you can change this behaviour by the following configuration:
<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>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
The compileReuseStrategy in contradiction is intended to define the behaviour in relationship with multi-threaded running of the compiler.
What about using mvn clean install?

Maven: Build code against Java 1.4, but build test code against Java 1.5

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>

Categories

Resources