4

I have set up a parent POM for use by all my projects with standard configuration. I've been playing with the maven-java-formatter-plugin and using my own formatter config file. In the parent POM I've set this up:

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

But this requires that I include the formatter-config.xml in each project. What I'd like to somehow do is automatically retrieve/copy that file from some known location (possibly another project whose only purpose is to hold this file) into all the projects automagically during the build phase. I'm pretty sure there's a way to do this, but I can't come up with the right search terms.

EDIT (response to prunge)

I put the config file in a separate project (packaged as a jar) and added that as a dependency to my parent POM. However, when I try to build a project that makes use of the parent POM, I get this error:

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

It appears that the formatter-config.xml file is not being resolved from the classpath as per the formatter plugin page you referenced.

This is what I have; perhaps you can tell me where I went wrong:

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

Parent POM is same as above but have added new dependency:

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

No changes to the TestServlet project POM; it includes the parent POM:

<parent>
    <groupId>my.company</groupId>
    <artifactId>super-pom</artifactId>
    <version>1.0</version>
</parent>
sdoca
  • 7,832
  • 23
  • 70
  • 127
  • 1
    Is the code-formatter dependency added as a dependency of the _maven-java-formatter-plugin_? It looks like you just have it as a dependency declared in the super POM, not within the plugin as it needs to be. – prunge Oct 13 '11 at 04:42
  • No, I had the dependency in the wrong spot. It's working now. Thanks! – sdoca Oct 14 '11 at 16:10

1 Answers1

4

See Multimodule Configuration example from the plugin site.

Essentially, you create a project which just has the formatter-config.xml file in it, build and deploy this project to your repository, then add this dependency to the plugin itself in your parent POM.

e.g.

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

where in your buildutils project, the formatter-config.xml file will put into src/main/resources/eclipse/ directory.

prunge
  • 22,460
  • 3
  • 73
  • 80
  • Following your directions, my project can't find the file. Please see my edit to the question for details. – sdoca Oct 12 '11 at 16:50
  • Great answer, ran into this while trying to setup a global spotbugs filter file. Was also able to get the filter file to import external xml entities from a file (https://stackoverflow.com/questions/5121052/can-we-import-xml-file-into-another-xml-file) which i pointed to a well known path that my child projects could create and add their own additional spotbugs filters. – PragmaticProgrammer Jul 16 '20 at 21:07