问题描述
当我忘记在Serializable
类中声明serialVersionUID时,我想使Maven构建失败.使用javac
,这很容易:
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
直接将其翻译为Maven不起作用:
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>
compilerArgument
用引号引起来,因此javac
仅接收一个包含-Xlint:serial -Werror
的参数,而不是将-Xlint:serial
和-Werror
作为单独的参数.因此,您阅读了文档,然后找到了compilerArguments
:
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>
这看起来很奇怪-冒号在Xlint
名称空间中创建了serial
元素,该元素在任何地方都没有声明-但它可以起作用,直到您要发布:
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:
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.
显然,常规POM读取器以不同于发布插件所使用的另一种方式处理XML名称空间.
Apparently, the regular POM reader handles XML namespaces in another way than the one used by the release plugin.
那么,当其中一些开关包含对于纯XML元素无效的字符而又不破坏发行插件时,我该如何传递javac
多个命令行开关?
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?
推荐答案
似乎在compilerArgument
中转义了空格,但引号却并非如此.因此,如果用引号将参数中的空格括起来,则会得到两个参数:
It seems that while spaces are escaped in compilerArgument
, the same isn't true for quotes. So, if you surround the spaces in the argument with quotes, you get two arguments:
<compilerArgument>-Xlint:serial" "-Werror</compilerArgument>
这将调用javac "-Xlint:serial" "-Werror"
而不是javac "-Xlint:serial -Werror"
.
在文档中我找不到任何东西.
There's nothing in the docs about this that I can find.
这篇关于如何在不破坏Maven发行插件的情况下传递javac多个命令行参数,其中一些包含冒号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!