简体   繁体   English

使用Maven程序集合并属性文件

[英]Merge properties files with Maven assembly

I have a problem with maven assembly plugin. 我有maven程序集插件的问题。

I have a maven project which use several jars. 我有一个使用几个罐子的maven项目。 Each jar contains configuration files. 每个jar包含配置文件。 With another project, I use maven assembly plugin to assemble all configurations in unique jar. 在另一个项目中,我使用maven程序集插件在独特的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. 我没有实现告诉maven合并两个文件而不是覆盖。

Someone knows how to do that ? 有人知道怎么做吗?

Thanks. 谢谢。

The maven-shade-plugin combined with the AppendingTransformer should do what you want. maven-shade-plugin与AppendingTransformer结合使用可以做你想要的。

We use it to merge together the properties files from two zip projects, defined as separate maven modules, into a single zip file. 我们使用它将两个zip项目(定义为单独的maven模块)的属性文件合并到一个zip文件中。 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. 我们还定义要合并的模块作为执行合并的maven模块的依赖项。

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>

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. 它不是你想要的,但我会使用http://maven.apache.org/plugins/maven-antrun-plugin/插件来运行ant concat任务http://ant.apache.org/manual/ Tasks / concat.html合并文件。 I would run the maven-antrun in prepare-package phase. 我会在准备包阶段运行maven-antrun。

Based on Skarab's answer, here's the code I used to solve this issue using the maven-antrun-plugin : 根据Skarab的回答,这是我用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>

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 这是一个关于stackoverflow的线程,其中记录了这样一个文件的重命名在Maven中重命名资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM