Merge properties files with Maven assembly

2019-01-24 09:42发布

I have a problem with maven assembly plugin.

I have a maven project which use several jars. Each jar contains configuration files. With another project, I use maven assembly plugin to assemble all configurations in unique jar.

All work fine but unfortunately, two files are the same name and the second overwrites the first.

I don't achieve to tell maven to merge the two files instead of overwrite.

Someone knows how to do that ?

Thanks.

4条回答
Root(大扎)
2楼-- · 2019-01-24 09:44

It is not exactly what you are looking for, but I would use http://maven.apache.org/plugins/maven-antrun-plugin/ plugin to run ant concat task http://ant.apache.org/manual/Tasks/concat.html to merge the files. I would run the maven-antrun in prepare-package phase.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-24 09:45

You could might try to rename the first file and merge the two files after that.

Here is a Thread on stackoverflow, where the renaming of such a file is documentated: Renaming resources in Maven

查看更多
老娘就宠你
4楼-- · 2019-01-24 09:53

The maven-shade-plugin combined with the AppendingTransformer should do what you want.

We use it to merge together the properties files from two zip projects, defined as separate maven modules, into a single zip file. This creates the superset of the files and directories from the two modules and merges together the specified properties file. We also define the module to merge as a dependency of the maven module doing the merge.

Something like this should do the trick:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>1.4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
        <goal>shade</goal>
          </goals>
          <configuration>
        <filters>
          <filter>
            <artifact>groupname:artifactname</artifact>
            <includes>
              <include>**/*</include>
            </includes>
          </filter>
        </filters>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>propertyfiletomerge.properties</resource>
          </transformer>
        </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
查看更多
放我归山
5楼-- · 2019-01-24 10:03

Based on Skarab's answer, here's the code I used to solve this issue using the maven-antrun-plugin:

<project>
...
<build>
    ...
    <plugins>
        ...
        <plugin> 
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                <phase>prepare-package</phase>
                <configuration>

                    <target>
                        <concat destfile="${project.build.directory}/setup_db.sql">
                            <fileset file="${project.basedir}/src/main/resources/db/sql_one/*.sql" />
                            <fileset file="${project.basedir}/src/main/resources/db/sql_another/*.sql" />
                        </concat>
                    </target>

                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
              </execution>
            </executions>
        </plugin>
        ... 
    </plugins>
</build>

查看更多
登录 后发表回答