将属性文件与 Maven 程序集合并

发布于 2024-10-30 23:19:41 字数 217 浏览 7 评论 0原文

我的 Maven 程序集插件有问题。

我有一个使用多个 jar 的 Maven 项目。每个 jar 包含配置文件。 在另一个项目中,我使用 Maven 组装插件将所有配置组装到独特的 jar 中。

一切正常,但不幸的是,两个文件同名,第二个文件覆盖第一个文件。

我没有告诉 Maven 合并两个文件而不是覆盖。

有人知道该怎么做吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

七月上 2024-11-06 23:19:41

maven-shade-plugin 与 AppendingTransformer 结合应该可以完成您想要的操作。

我们使用它将来自两个 zip 项目(定义为单独的 Maven 模块)的属性文件合并到一个 zip 文件中。这将创建两个模块中的文件和目录的超集,并将指定的属性文件合并在一起。我们还将要合并的模块定义为执行合并的 Maven 模块的依赖项。

像这样的事情应该可以解决问题:

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

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>
叫思念不要吵 2024-11-06 23:19:41

这并不完全是您正在寻找的,但我会使用 http://maven。 apache.org/plugins/maven-antrun-plugin/ 运行 ant concat 任务的插件 http://ant.apache.org/manual/Tasks/concat.html 合并文件。我将在 prepare-package阶段。

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.

那支青花 2024-11-06 23:19:41

根据 Skarab 的回答,这是我使用 maven-antrun- 解决此问题的代码插件

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

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>

浪菊怪哟 2024-11-06 23:19:41

您可以尝试重命名第一个文件,然后合并两个文件。

这是 stackoverflow 上的一个线程,其中记录了此类文件的重命名:
在 Maven 中重命名资源

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文