multiple src/main/resources folders without profil

2019-07-26 23:36发布

I'm working on trying to have my project automatically build two different versions of a project. What I have done is I have added two different plug-in executions as follows

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>exploded</goal>
                    </goals>
                </execution>                
            </executions>
            <configuration>
                <resources>
                    <resource>
                    <directory>src/main/resources/first</directory>
                    </resource>
               </resources>
                <webappDirectory>${webappDirectory}</webappDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>war</goal>
                    </goals>
                </execution>                
            </executions>
            <configuration>
                <resources>
                    <resource>
                        <directory>src/main/resources/second</directory>
                    </resource>
               </resources>
                <webappDirectory>${project.build.directory}/deployment</webappDirectory>
            </configuration>
        </plugin>

It builds both webappDirectories and it created the war file as expected, but the resources doesn't get included correctly (it seems to ignore the resource definitions above).

I am guessing its because I am doing this in the wrong phase, at what phase do I need to override the default resources directory?

(please notice, I do not wish to have two different profiles, I want to have both done at once)

1条回答
相关推荐>>
2楼-- · 2019-07-27 00:40

I have wrote an blog entry exactly about that problem but the essence is about to have the maven project (in your case the war module) with the following structure:

.
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |-- resources
    |   |-- environment
    |   |   |-- test
    |   |   |   `-- database.properties
    |   |   |-- qa
    |   |   |   `-- database.properties
    |   |   `-- production
    |   |       `-- database.properties
    |   `-- webapp

The most important thing is to create the different configuration via the maven-assembly-plugin with a configuration like this:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <id>test</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
      <execution>
        <id>qa</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
      <execution>
        <id>production</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
    </executions>
  </plugin>

what you also need are assembly descriptors like the following:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

  <id>test</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/test/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

Of course you have to name them accordingly and obviously you have to change the directory within the descriptor as well. Based on that it is now possible to create multiple artifacts (in your case war files) with different configurations during the package cycle. So you simply call your project build via:

  mvn clean package

Finally i recommend not to use the src/main/resources folder, cause that folder should be used for production information which does not change for the different configuration. Furthermore the src/test/resources will be used for the test resources.

查看更多
登录 后发表回答