Overwrite resource file with maven assembly plugin

怎甘沉沦 提交于 2019-12-10 04:13:37

问题


I use maven-assembly-plugin with "jar-with-dependencies" to package jar. There are 2 dependencies artifact having log-back.xml. The second artifact is depend on the first one. I want to have log-back.xml of the second artifact in final jar, but it always contain log-back.xml of the first one. So how can I control this?

Thanks


回答1:


You can use the unpackOptions to achieve this. Try something like the following:

<assembly>
...
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${groupId}:${artifact.whose.logback.is.to.be.excluded} </include>
            </includes>
            <unpack>true</unpack>
            <unpackOptions>
                <excludes>
                    <exclude>**/logback.xml</exclude>
                </excludes>
            </unpackOptions>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <excludes>
                <exclude>${groupId}:${artifact.whose.logback.is.to.be.excluded}</exclude>
            </excludes>
            <unpack>true</unpack>
        </dependencySet>
    </dependencySets>
</assembly>



回答2:


Is the first artifact a module of your own project? If so, you could exclude the log-back.xml there in the resources section of the pom.xml.

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <excludes>
      <exclude>log-back.xml</exclude>
    </excludes>
  </resource>
  ...
</resources>

However, this only works if this module does not require the log-back.xml by itself when it is built out of the scope of the overall jar.




回答3:


(With last version of maven-assembly-plugin at this time : 3.0.0)

I had the same problem with an assembly build.

I had tow dependencies with the same properties file but with a different content (one good and the other overwritting the first with missing declarations).

The problem was that i finally had the bad configuration file replacing the other in my assembly jar.

The only cleanest solution i found to overwrite the file was to :

1 - Add the good file that i wanted to keep for the build in my project : ex: src/main/resources/META-INF/services/myfileWhichOverwriteTheBadDependenciesRessources.xml

2 - Add a fileset with 'filtered' setted to 'true' in my assembly descriptor :

    <fileSet>
        <directory>${project.main.resources}/META-INF</directory>
        <outputDirectory>META-INF</outputDirectory>
        <filtered>true</filtered>
    </fileSet>

(the 'project.main.resource' property is setted to 'src/main/resources' in my case)



来源:https://stackoverflow.com/questions/6027519/overwrite-resource-file-with-maven-assembly-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!