java 使用 Maven 程序集合并属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5598314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge properties files with Maven assembly
提问by Kiva
I have a problem with maven assembly plugin.
我有 maven 程序集插件的问题。
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.
我有一个使用几个 jar 的 Maven 项目。每个 jar 都包含配置文件。在另一个项目中,我使用 maven assembly 插件将所有配置组装到唯一的 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.
谢谢。
采纳答案by Skarab
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.htmlto merge the files. I would run the maven-antrun in prepare-packagephase.
这不完全是您要查找的内容,但我会使用http://maven.apache.org/plugins/maven-antrun-plugin/插件来运行 ant concat 任务http://ant.apache.org/manual/ Tasks/concat.html合并文件。我会在准备包阶段运行 maven-antrun 。
回答by Jon Denly
The maven-shade-plugincombined with the AppendingTransformer should do what you want.
与 AppendingTransformer 相结合的maven-shade-plugin应该可以满足您的需求。
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.
我们使用它将来自两个 zip 项目(定义为单独的 maven 模块)的属性文件合并到一个 zip 文件中。这将创建来自两个模块的文件和目录的超集,并将指定的属性文件合并在一起。我们还将要合并的模块定义为执行合并的 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>
回答by Vincent
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>
回答by L.Butz
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 上的一个线程,其中记录了此类文件的重命名: Renaming resources in Maven