Conditionally exclude some resources in maven from war

前端 未结 2 1738
Happy的楠姐
Happy的楠姐 2021-01-21 02:40

I developed an application, and its final war was OK in a development mode but it contains some configuration files which I need to externalize for production. I was able to man

相关标签:
2条回答
  • 2021-01-21 03:13

    Please DON'T use profiles for this. The problem with profiles is that the resulting artifact contains no information about the profiles used to build it.

    Basic rule: A profile must not change the result of the build.

    Or the other way around: If you build the same source code revision, the result must be identical, regardless of profiles or arguments.

    (see How to really use Maven profiles (without endangering your karma) for more information)

    So, a better way to do it would be to create two war modules, say my-project and my-project-dev.war.

    If my-project-dev.war has a war-dependency on my-project.war, it will use your production war as overlay, resulting in a combined war files using your production and your development time resources.

    Added bonus feature: in your development war, you can override entries of your production war (say, logging-config).

    0 讨论(0)
  • 2021-01-21 03:27

    I end up with the following solution. It does not use profiles, which are considered as antipattern by some guys: blackbuild and Henrik Larne. The solution is built with classifiers and maven war resources exclusion. It will produce two wars that differ with their content and file name.

    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
            <classifier>dev</classifier>
            <webappDirectory>${project.build.directory}/${project.build.finalName}-dev</webappDirectory>
        </configuration>
        <executions>
            <execution>
                <id>package-prod</id>
                <phase>package</phase>
                <configuration>
                    <classifier>prod</classifier>
                    <webappDirectory>${project.build.directory}/${project.build.finalName}-prod</webappDirectory>
                    <packagingExcludes>WEB-INF/classes/*.jks,WEB-INF/classes/some.properties</packagingExcludes>
                </configuration>
                <goals>
                    <goal>war</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    Thanks everybody.

    0 讨论(0)
提交回复
热议问题