多个没有配置文件的src/main/resources文件夹(我希望一次构建多个版本) [英] multiple src/main/resources folders without profiles (I wish to build multiple versions at once)

查看:80
本文介绍了多个没有配置文件的src/main/resources文件夹(我希望一次构建多个版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的项目自动构建一个项目的两个不同版本.我所做的是,我添加了以下两个不同的插件执行程序

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>

它同时构建了webappDirectories并创建了war文件,但是资源未正确包含(似乎忽略了上面的资源定义).

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)

推荐答案

我写了一个

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

最重要的是,通过 maven-assembly-plugin创建不同的配置具有这样的配置:

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>

当然,您必须相应地命名它们,并且显然您还必须在描述符中更改目录.基于此,现在可以在打包周期中使用不同的配置创建多个工件(在您的情况下为war文件).因此,您只需通过以下方式调用项目构建:

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

最后,我建议不要使用src/main/resources文件夹,因为该文件夹应用于生产信息,对于其他配置,该信息不会更改.此外,src/test/resources将用于测试资源.

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.

这篇关于多个没有配置文件的src/main/resources文件夹(我希望一次构建多个版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆