javamaventomcatbuildwar

How to prevent MVN from changing file dates as it packages a WAR file?


I have a TomCat application with a lot of JSP files. I recently switched from Gradle to MVN for doing the build. One big difference is that the MVN build sets the timestamps on all the JSP files to be the time of the build. I don't want this for two reasons. First it causes unnecessary transfer to the service of the unpacked JSP files, because the timestamp suggests they have changed when they have not. Second is that during testing/debug phase I edit the JSP files on the TomCat server, and I need the timestamp to accurately show the date of the last change of the file.

I saw a hint somewhere that this might be due to MVN changing the line ending from Unix to Windows or whatever. I don't want it doing that either. I saw a setting for <lineEnding>KEEP</lineEnding> and I have put this in the POM file in various places, but no effect. I don't know if this is the problem or not.

Here is my entire POM file if that helps

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.purplehillsbooks.purple/purple -->
    <dependency>
        <groupId>com.purplehillsbooks.purple</groupId>
        <artifactId>purple</artifactId>
        <version>3.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.17.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

            
    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-catalina</artifactId>
        <version>11.0.0-M15</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.14</version>
    </dependency>

    
  </dependencies>
  
  <build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>8</source>
                <target>8</target>
                <lineEnding>KEEP</lineEnding>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <lineEnding>keep</lineEnding>
                <attachClasses>true</attachClasses>
                <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                <webResources>
                    <resource>
                        <directory>src/main/webapp</directory>
                        <filtering>true</filtering>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
        
    </plugins>
  </build>

</project>

Solution

  • Two suggestions for you. The first is, if you need all of the configuration for the maven-war-plugin then remove the filtering command. Maven is doing what you tell it to - read each file and look to see if there are substitutions. It writes the new file out as instructed.

    But your configuration is standard - I'd just have:

    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.2</version>
    </plugin>
    

    and it will default to what you want - the JSP will maintain the timestamp that it originally had.