0

I'm attempting to build a war with one of two different web.xml files based on the build profile. I thought it would work if I used the resource tag but it's putting the web.xml file into the target/WEB-INF/classes directory and I need it in the target/WEB-INF directory. Here's a snippet of my pom:

    <profile>
        <id>war_ssl</id>
        <modules>
            <module>ess-spring</module>
        </modules>
        <build>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/resources</directory>
                </resource>
                <resource>
                    <directory>${basedir}/src/main/web_xml/ssl</directory>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>war_no_ssl</id>
        <modules>
            <module>ess-spring</module>
        </modules>
        <build>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/resources</directory>
                </resource>
                <resource>
                    <directory>${basedir}/src/main/web_xml/no_ssl</directory>
                </resource>
            </resources>
        </build>
    </profile>

Note that there is only one file in each dir, ${basedir}/src/main/web_xml/ssl and ${basedir}/src/main/web_xml/no_ssl and that's a different version of web.xml.

Very specifically, I want all resources with the exception of one file (web.xml) in both profiles and just one of the specific web.xml files depending on the selected profile. And I can't figure out how to tell it to write the web.xml file into the WEB-INF directory. It's going into the WEB-INF/classes directory. Please let me know if what I want to do is even possible in Maven (and how to do it if it is).

================================================

It turns out that Wemu's suggestion below did \what I wanted. Here is the revised, snippet:

    <profile>
        <id>war_ssl</id>
        <modules>
            <module>ess-spring</module>
        </modules>
        <build>
             <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <webXml>${basedir}/src/main/web_xml/ssl/web.xml</webXml>
                    </configuration>
                </plugin>
            </plugins> 
        </build>
    </profile>
    <profile>
        <id>war_no_ssl</id>
        <modules>
            <module>ess-spring</module>
        </modules>
        <build>
             <plugins>
                 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <webXml>${basedir}/src/main/web_xml/no_ssl/web.xml</webXml>
                    </configuration>
                </plugin>
            </plugins> 
        </build>
    </profile>

The war.xml file is in the correct place now. Thanks Wemu!!!

  • 1
    haven't tried this as I prefer to not let profiles modify the resulting artifact, but: the war plugin has a property which web.xml to use: https://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#webXml - the profile would only need to contain the location of that file. – wemu Sep 12 '17 at 21:23
  • Wemu, that worked almost perfectly. It put the web.xml file where I wanted and allowed me to remove the resources tag as well. Unfortunately the file now looks like a binary file (see above). – user3926339 Sep 12 '17 at 22:16
  • A better solution may become evident if you explain the problem you're attempting to solve. As @wemu says, profiles should generally be avoided if you can. – Steve C Sep 13 '17 at 02:45
  • 1
    is src/main/web_xml/no_ssl a folder or a file? according to the docs webXml needs to point to a file – wemu Sep 13 '17 at 08:11
  • Steve, I thought I had stated the problem. I'll try a different way: I want to build a war file with one of two differnt web.xml files. So I want a way to choose which of the two to use. After looking around quite a bit I thought I could do it via resources tags but those ended up putting the web.xml file in the wrong directory. Did you have a suggestion? – user3926339 Sep 13 '17 at 21:13
  • Wemu, ya I didn't append '/web.xml' as it seemed redundant. But once I did the file came out in the war correctly. Thanks! – user3926339 Sep 13 '17 at 21:17
  • Wemu, I had previously not appended '/web.xml' to the path as it seemed redundant but after your suggestion that I do so the file does indeed turn up in the war correct. Thanks!!! As a side note, this work relates to converting an old project authored years ago that I'm attempting to convert from ant to maven. Hilariously it turns out that the different web.xml files are unnecessary as what they were attempting to do (turn on or off ssl) can be done on the server without any configuration requirement in the war. But after I started down this path I really wanted to find out how to do this. – user3926339 Sep 13 '17 at 21:20

0 Answers0