How to transform web.xml during packaging with mav

2019-06-07 13:36发布

I am porting an ant script to maven, and I am stuck on transforming my web.xml.

My web.xml has the following security constraint set to NONE during development, and to CONFIDENTIAL for the war that is produced for the production server.

<security-constraint>
    <web-resource-collection>
      <web-resource-name>HTTPSOnly</web-resource-name>
      <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

I use an XSL script to process web.xml file which is called from ant. I want maven to produce war file where the XSL has been applied to the web.xml and the transformed web.xml is used in the generated war.

I know about the maven XSL plugin and the concept of maven profiles but I am struggling to put it all together and I am not sure what the right maven way is for dealing with situation.

标签: maven maven-3
3条回答
冷血范
2楼-- · 2019-06-07 14:09

There are many ways you can achieve this. Probably the simplest way is to use maven-antrun-plugin http://maven.apache.org/plugins/maven-antrun-plugin/ and call your existing ant task

Other ways I know is to use a variable / placeholder and Maven filtering (variable substitution), have a look at: http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html

查看更多
Animai°情兽
3楼-- · 2019-06-07 14:20

I solved my problem my setting up a prod profile with the pom.xml for the web app, with the following plugins in it. This works for me because when the project is imported into eclipse I really don't want anything fancy to happen and I want everything to work out of the box the developer machine. It is not shown below but I also added a call to NodeJS to build the javascript front end using RequireJS at the generate-sources phase.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xml-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>transform</goal>
            </goals>
            <configuration>
                <transformationSets>
                    <transformationSet>
                        <dir>src/main/webapp/WEB-INF</dir>
                        <includes>
                            <include>web.xml</include>
                        </includes>
                        <stylesheet>src/main/webapp/WEB-INF/web.xsl</stylesheet>
                    </transformationSet>
                </transformationSets>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webXml>target/generated-resources/xml/xslt/web.xml</webXml>
        <archiveClasses>true</archiveClasses>
        <warSourceExcludes>META-INF/context.xml</warSourceExcludes>
        <packagingExcludes>
            **/**/context.xml
        </packagingExcludes>
    </configuration>
</plugin>
查看更多
放荡不羁爱自由
4楼-- · 2019-06-07 14:27

The Maven way is to use filtering.

Example below of regular web.xml filtering, without xsl:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <webResource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <includes>
                            <include>web.xml</include>
                        </includes>
                        <targetPath>WEB-INF</targetPath>
                        <filtering>true</filtering>
                    </webResource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
    <filters>
        <filter>src/main/filters/filter-${env}.properties</filter>
    </filters>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

If you insist on xsl, you could use Maven xml plugin, working from the base of the example:

 <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>xml-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>transform</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <transformationSets>
            <transformationSet>
              <dir>src/main/xml</dir>
              <stylesheet>src/main/stylesheet.xsl</stylesheet>
            </transformationSet>
          </transformationSets>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
查看更多
登录 后发表回答