Maven:如何在父POM中包含文件?

4

我已经为所有项目设置了一个父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。我希望能够自动从某个已知位置(可能是另一个仅用于保存此文件的项目)检索/复制该文件到所有项目中,然后在构建阶段自动执行。我相信有一种方法可以实现这一点,但我找不到正确的搜索词语。
编辑(响应prunge)
我将配置文件放在一个单独的项目(打包为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

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

1
代码格式化程序的依赖项是否已添加为“maven-java-formatter-plugin”的依赖项?看起来你只是将其作为超级POM中声明的依赖项,而不是在插件内部声明。 - prunge
不,我的依赖项放错了位置。现在它可以工作了。谢谢! - sdoca
1个回答

4

请参考插件网站上的多模块配置示例

基本上,您需要创建一个仅包含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项目中,格式化程序配置文件将放置在src/main/resources/eclipse/目录中。

根据您的指示,我的项目找不到文件。请查看我在问题中的编辑以获取详细信息。 - sdoca
非常好的答案,我在尝试设置全局spotbugs过滤器文件时遇到了这个问题。我还能够从一个文件中导入外部xml实体到过滤器文件中(https://dev59.com/Tm435IYBdhLWcg3w9E9Y),我指向了一个众所周知的路径,我的子项目可以创建并添加自己的额外的spotbugs过滤器。 - PragmaticProgrammer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接