I want to make my Maven build fail when I forget to declare serialVersionUIDs in a Serializable
class. With javac
, that's easy:
$ javac -Xlint:serial -Werror Source.java
Directly translating that to Maven doesn't work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<compilerArgument>-Xlint:serial -Werror</compilerArgument>
</configuration>
</plugin>
The compilerArgument
is quoted, so javac
receives only one argument, containing -Xlint:serial -Werror
, instead of -Xlint:serial
and -Werror
as separate arguments. So you read the docs, and find compilerArguments
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<compilerArguments>
<Xlint:serial />
<Werror />
</compilerArguments>
</configuration>
</plugin>
This looks weird - the colon makes serial
element in the Xlint
namespace, which isn't declared anywhere - but it works... until you want to do a release:
$ mvn release:prepare
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.3.2:prepare (default-cli) on project my-project: Error reading POM: Error on line 58: The prefix "Xlint" for element "Xlint:serial" is not bound.
Apparently, the regular POM reader handles XML namespaces in another way than the one used by the release plugin.
So how do I pass javac
multiple command-line switches when some of those switches contain characters which aren't valid for plain XML elements, without breaking the release plugin?
The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine.
The javac command reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. The javac command can also process annotations in Java source files and classes.
The output of javac is always classfiles, and the name of the file matches the name of the class contained within it. (A source file with multiple classes in will result in multiple output files.)
See http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerArgs
and http://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html
Maven 3.1 or later
<source>1.6</source>
<target>1.6</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</processors>
<compilerArgs>
<arg>-verbose</arg>
<arg>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</arg>
</compilerArgs>
or Maven 3.0 or older
<compilerArguments>
<verbose />
</compilerArguments>
<compilerArgument>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</compilerArgument>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With