【问题标题】:Maven: Include file defined in parent POM?Maven:包含在父 POM 中定义的文件?
【发布时间】:2011-12-05 17:24:04
【问题描述】:

我已经设置了一个父 POM,供我所有具有标准配置的项目使用。我一直在玩maven-java-formatter-plugin 并使用我自己的格式化程序配置文件。在父 POM 中我已经设置了这个:

    <build>
    ...
        <!-- Format the code during process-sources -->
        <plugin>
            <groupId>com.googlecode.maven-java-formatter-plugin</groupId>
            <artifactId>maven-java-formatter-plugin</artifactId>
            <version>${formatter.version}</version>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>format</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- Use plugin to compiler config vice those in formatter config 
                    file -->
                <overrideConfigCompilerVersion>true</overrideConfigCompilerVersion>
                <compilerSource>${jdk.version}</compilerSource>
                <compilerCompliance>${jdk.version}</compilerCompliance>
                <compilerTargetPlatform>${jdk.version}</compilerTargetPlatform>
                <!-- Use Unix line endings -->
                <lineEnding>LF</lineEnding>
                <!-- The formatter config file -->
                <configFile>${project.basedir}/formatter-config.xml</configFile>
            </configuration>
        </plugin>
    </plugins>
</build>

但这需要我在每个项目中包含 formatter-config.xml。我想做的是在构建阶段自动从某个已知位置(可能是另一个项目,其唯一目的是保存该文件)自动检索/复制该文件到所有项目中。我很确定有办法做到这一点,但我想不出正确的搜索字词。

编辑(对删减的响应)

我将配置文件放在一个单独的项目中(打包为 jar)并将其添加为我父 POM 的依赖项。但是,当我尝试构建一个使用父 POM 的项目时,我收到此错误:

[ERROR] Failed to execute goal com.googlecode.maven-java-formatter-plugin:maven-java-formatter-plugin:0.3.1:format (default) on project TestServlet: Config file [eclipse/formatter-config.xml] cannot be found: Could not find resource 'eclipse/formatter-config.xml'. -> [Help 1]

根据您引用的格式化程序插件页面,似乎没有从类路径中解析 formatter-config.xml 文件。

这就是我所拥有的;也许你可以告诉我哪里出错了:

CodeFormatter (builds a jar)
|-- pom.xml
|-- src
|   `-- main
|       `-- resources
|           `-- eclipse
|               `-- formatter-config.xml

父 POM 同上,但增加了新的依赖:

<groupId>my.company</groupId>
<artifactId>super-pom</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
....
<dependencies>
    <dependency>
        <groupId>my.company</groupId>
        <artifactId>code-formatter</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>
...

TestServlet 项目 POM 没有变化;它包括父 POM:

<parent>
    <groupId>my.company</groupId>
    <artifactId>super-pom</artifactId>
    <version>1.0</version>
</parent>

【问题讨论】:

  • code-formatter 依赖是否添加为 maven-java-formatter-plugin 的依赖?看起来您只是将它作为在超级 POM 中声明的依赖项,而不是在插件中,因为它需要。
  • 不,我把依赖放在了错误的位置。它现在正在工作。谢谢!

标签: file maven include pom.xml


【解决方案1】:

从插件站点查看Multimodule Configuration example

基本上,您创建一个项目,其中仅包含 formatter-config.xml 文件,构建并部署该项目到您的存储库,然后将此依赖项添加到父 POM 中的 插件本身

例如

<plugin>
    <groupId>com.googlecode.maven-java-formatter-plugin</groupId>
    <artifactId>maven-java-formatter-plugin</artifactId>
    <version>${formatter.version}</version>
    <executions>
        <execution>
            <phase>process-resources</phase>
            <goals>
                <goal>format</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!-- Use plugin to compiler config vice those in formatter config 
            file -->
        <overrideConfigCompilerVersion>true</overrideConfigCompilerVersion>
        <compilerSource>${jdk.version}</compilerSource>
        <compilerCompliance>${jdk.version}</compilerCompliance>
        <compilerTargetPlatform>${jdk.version}</compilerTargetPlatform>
        <!-- Use Unix line endings -->
        <lineEnding>LF</lineEnding>
        <!-- The formatter config file -->
        <configFile>eclipse/formatter-config.xml</configFile>
    </configuration>
    <dependencies>
      <dependency>
        <groupId>com.myorg.buildutils</groupId>
        <artifactId>my-eclipse-config</artifactId>
        <version>1.0</version>
      </dependency>
    </dependencies>
</plugin>

在您的 buildutils 项目中,formatter-config.xml 文件将放入 src/main/resources/eclipse/ 目录中。

【讨论】:

  • 按照您的指示,我的项目找不到该文件。有关详细信息,请参阅我对问题的编辑。
  • 很好的答案,在尝试设置全局污点过滤器文件时遇到了这个问题。还能够获取过滤器文件以从文件 (stackoverflow.com/questions/5121052/…) 中导入外部 xml 实体,该文件我指向一个众所周知的路径,我的子项目可以创建并添加他们自己的额外 spotbugs 过滤器。
猜你喜欢
  • 2011-06-17
  • 2011-12-10
  • 1970-01-01
  • 2012-09-28
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
相关资源
最近更新 更多