如何在不破坏 Maven 发布插件的情况下传递 javac 多个命令行参数,其中一些包括冒号? [英] How do I pass javac multiple command-line arguments, some of which include colon, without breaking Maven release plugin?

查看:31
本文介绍了如何在不破坏 Maven 发布插件的情况下传递 javac 多个命令行参数,其中一些包括冒号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我忘记在 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:

$ mvn release:prepare

org.apache.maven.lifecycle.LifecycleExecutionException:无法在项目 my-project 上执行目标 org.apache.maven.plugins:maven-release-plugin:2.3.2:prepare (default-cli):读取 POM 时出错:第 58 行出错:未绑定元素Xlint:serial"的前缀Xlint".

显然,常规 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".

This invokes javac "-Xlint:serial" "-Werror" rather than javac "-Xlint:serial -Werror".

我在文档中找不到任何关于此的内容.

There's nothing in the docs about this that I can find.

这篇关于如何在不破坏 Maven 发布插件的情况下传递 javac 多个命令行参数,其中一些包括冒号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆