java - 如何在不破坏 Maven 发布插件的情况下传递 javac 多个命令行参数,其中一些参数包括冒号?

标签 java maven javac maven-release-plugin maven-compiler-plugin

当我忘记在 Serializable 类中声明 serialVersionUID 时,我想让我的 Maven 构建失败。使用 javac,这很容易:

$ javac -Xlint:serial -Werror Source.java

直接将其转换为 Maven 是行不通的:

        <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:

        <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 元素,它没有在任何地方声明 - 但它有效......直到你想做一个发布:

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

显然,常规 POM 阅读器处理 XML 命名空间的方式与发布插件使用的方式不同。

那么,当其中一些开关包含对纯 XML 元素无效的字符时,我该如何传递 javac 多个命令行开关,而不破坏发布插件?

最佳答案

参见 http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerArgs

http://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html

Maven 3.1 或更高版本

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

或 Maven 3.0 或更早版本

      <compilerArguments>
        <verbose />
      </compilerArguments>
      <compilerArgument>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</compilerArgument>

关于java - 如何在不破坏 Maven 发布插件的情况下传递 javac 多个命令行参数,其中一些参数包括冒号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11339056/

相关文章:

java - 如何在tts说话时停止语音识别?

java - 如何使用 gvNix 典型安全性?入门不起作用

java - 如何使用 Spring 4 MVC 配置 Jackson Streaming API

java - "CTRL + SHIFT + o"组织导入在 STS 中不起作用

java.lang.UnsupportedClassVersionError - 不同版本的 JDK 和 JRE

Javac 找不到位于同一目录中的类

java - 验证 jar 文件 : jar not loaded servlet

java - 找不到部署 "war"的子部署 "ear"中命名的持久性单元

java - 无法删除 slf4j 依赖项

maven-compiler-plugin 指定 fork=true 时不做注解处理?