Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass javac multiple command-line arguments, some of which include colon, without breaking Maven release plugin?

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?

like image 585
gustafc Avatar asked Jul 05 '12 06:07

gustafc


People also ask

What happens when we write the javac command with filename on the command prompt?

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.

Which of these commands is used for compiling a Java source code to produce a byte code?

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.

What does the output of javac contain?

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.)


1 Answers

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>
like image 160
Kalpesh Soni Avatar answered Oct 23 '22 09:10

Kalpesh Soni