Maven: How to specify Javac plugin argument with m

2019-04-11 11:47发布

Javac provides the following nonstandard option (from "javac -X" command line help):

-Xplugin:"name args"       Name and optional arguments for a plug-in to be run

However, Maven does not handle that format. For example, the following does not work:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <compilerArgs>
      <arg>-Xplugin:"Manifold static"</arg>
    </compilerArgs>
  </configuration>
</plugin>

I've tried several different variations and tried replacing the space with '=', still no bueno. Is there a workaround?

Note I am using the Manifold javac plugin with Java 8. Reproducing is simple.. create a pom with a dependency on the manifold-all jar:

<dependency>
  <groupId>systems.manifold</groupId>
  <artifactId>manifold-all</artifactId>
  <version>0.9-alpha</version>
</dependency>

You don't really need any source, this is just to demonstrate that Maven will not accept the -Xplugin:"Manifold static" javac argument.

Also note Java 9 appears to have changed the format of -Xplugin to no longer require quotes. See the Java 9 docs on this.

1条回答
可以哭但决不认输i
2楼-- · 2019-04-11 12:32

In light of this discussion: MCOMPILER-178 and this patch: pull request, and since a space doesn't seem to be a character allowed in XML-names, I would suspect that you have to just drop all the double-quotes, and keep all the spaces.

This here:

myOption=foo"bar"baz

is string concatenation in bash (or whatever shell your are using). It stores the string "foobarbaz" in variable myOption. If you use double quotes in an option in bash, then it simply means: "bash, please treat the entire thing between the quotes as a single argument, don't split it into an array of string arguments". For example, if you run the following bash script:

#!/bin/bash

echo $1
echo $2
echo $3

with these arguments:

$ ./testArgs.sh hello -World:"blah blah blah" foobar

you will get this output:

hello
-World:blah blah blah
foobar

The -World:blah blah blah part is a single string, the quotes are not preserved.

Maven is not bash. POM is an XML file, it has tags, some of the tags are filled with text. XML doesn't care about bash's syntax for string concatenation or argument-parsing. If I understood the above discussion correctly, they passed the content of the xml-tags directly to the compiler, without any modifications.

Therefore, my best guess would be to just drop the double quotes:

<arg>-Xplugin:MyPlugin myArg</arg>

If this doesn't help, then I misunderstood the discussion linked above entirely, or maybe you are using a maven version that works differently.

查看更多
登录 后发表回答