How to copy a resource or another in Maven depending on the target environment?

后端 未结 5 1181
有刺的猬
有刺的猬 2020-12-07 15:19

I have to create test war and production war, which will have a different log4j.properties file in the WEB-INF directory. I have these files

相关标签:
5条回答
  • 2020-12-07 15:19

    The code above didn't work for me - had to change it to the following:

    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-prod-resources</id>
          <phase>process-resources</phase>
          <goals>
             <goal>copy-resources</goal>
          </goals>
          <configuration>
            <!-- this is important -->
            <overwrite>true</overwrite>
            <!-- this as well (target/ was missing) -->
            <outputDirectory>${basedir}/target/classes</outputDirectory>
            <resources>
              <resource>
                <directory>src/main/resources/prod</directory>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-12-07 15:20
    • Use Maven profiles (http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
    • Create a "dev" and "prod" profile, selecting an alternate set of resources for each profile. Make Dev active by default.

      <profiles>
          <profile>
              <id>dev</id>
              <activation>
                  <activeByDefault>true</activeByDefault>
              </activation>
              <build>
                  <resources>
                      <resource>
                          <directory>src/main/resources/dev</directory>
                      </resource>
                  </resources>
              </build>
          </profile>
          <profile>
              <id>prod</id>
              <build>
                  <resources>
                      <resource>
                          <directory>src/main/resources/prod</directory>
                      </resource>
                  </resources>
              </build>
          </profile>
      </profiles>
      
    • Build using the desired profile via: mvn install -Pdev or mvn install -Pprod

    0 讨论(0)
  • 2020-12-07 15:39

    The alternative way is to use maven-antrun-plugin

    <build>
        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-antrun-plugin</artifactId>
              <version>1.7</version>
              <executions>
                <execution>
                  <phase>validate</phase>
                  <goals>
                    <goal>run</goal>
                  </goals>
                  <configuration>
                    <tasks>
                      <echo>build.env: ${build.env} </echo>
                      <delete file="src/main/resources/log4j.properties" />
                      <copy file="src/env/${build.env}/log4j.properties"
                            tofile="src/main/resources/log4j.properties" />
                    </tasks>
                  </configuration>
                </execution>
              </executions>
            </plugin>
        </plugins>
    </build>
    

    Assume resource files are in following structure:

    src/
      env/
          dev/
              log4j.properties
          local/
              log4j.properties
          prod/
              log4j.properties
    

    When do maven build, run the following commands per environment:

    mvn package -Dbuild.env=dev
    mvn package -Dbuild.env=local
    mvn package -Dbuild.env=prod
    
    0 讨论(0)
  • 2020-12-07 15:44

    Last response is working. But you need to give the version to make it work.

    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.3</version>
      <executions>
        <execution>
          <id>copy-prod-resources</id>
          <phase>process-resources</phase>
          <goals>
             <goal>copy-resources</goal>
          </goals>
          <configuration>
            <!-- this is important -->
            <overwrite>true</overwrite>
            <!-- target -->
            <outputDirectory>${basedir}/target/classes</outputDirectory>
            <resources>
              <resource>
                <!-- source -->
                <directory>src/main/resources/prod</directory>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-12-07 15:46

    I solved this using the maven-resources plugin, where i created the prod directory which has the resources for production environment and copied them to WEB-INF/classes directory in process-resources phase.

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-resources-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <id>copy-prod-resources</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>webapp/WEB-INF/classes</outputDirectory>
                    <resources>
                        <resource>
                            <directory>src/main/resources/prod</directory>
                            <filtering>true</filtering>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
提交回复
热议问题