Changing packaging based on active profile in pom

前端 未结 3 660
北荒
北荒 2020-12-01 13:39

I have a project which I compile with maven. I have different profiles declared in pom.xml. For some of these profiles, I prefer building a war, and for other profiles I pre

相关标签:
3条回答
  • 2020-12-01 14:09

    If you want to use profile you can use something like:

    <?xml version="1.0" encoding="UTF-8"?>
    <project>
        <modelVersion>4.0.0</modelVersion>
        ..
        <packaging>${packaging.type}</packaging>
    
        <profiles>
            <profile>
                <id>webapp</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <packaging.type>war</packaging.type>
                </properties>
            </profile>
            <profile>
                <id>batch</id>
                <properties>
                    <packaging.type>jar</packaging.type>
                </properties>
                    </profile>
              </profiles>
    </project>
    
    0 讨论(0)
  • 2020-12-01 14:10

    Have you looked at the attachClasses configuration setting of the war plugin? This simple setting would let you build both a war and a jar (by default with the classifier "classes") in a single maven execution.

    0 讨论(0)
  • 2020-12-01 14:17

    I don't think you can.

    Two alternatives I can think of:

    • have two separate modules for packaging, and call one of those depending on profile
    • have your module in war and tweak the lifecycle (include/exclude build steps) depending on profile to produce your jar or your war

    I like the second approach better - a build server would probably build both, and a developer would use the proper profiles/settings to skip the unwanted type.

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