【问题标题】:Maven building for different environments针对不同环境的 Maven 构建
【发布时间】:2014-08-21 19:13:05
【问题描述】:

我有一个 Java 项目,目前可以很好地使用 Maven 构建。但是,我正在使用 Ant 脚本来完成我认为 Maven 也可以完成的其他任务。为了简化构建工作流程,我想让 Maven 做我需要的一切。这一切都由 Jenkins 自动完成。

挑战在于我希望 Maven 构建单个工件并通过命令行按需将其部署到多个环境。目前,我通过使用 Maven 进行构建,然后使用 Ant 脚本来创建 WAR 工件的副本,其中一些文件会根据我选择的环境进行更改。这适用于各种环境中不同的 JDBC 连接字符串、属性文件等。

我了解 Maven 可以使用配置文件,如 answer 中所述。但是,我一直在弄清楚如何为环境制作辅助 WAR 文件。例如我从构建步骤创建的“myproject.war”开始,我想创建“myproject-dev.war”,然后将其部署到开发 Tomcat 服务器。我想我可以使用 antrun maven 插件来更改文件,但我不知道如何将它挂钩到生命周期中。然后,我需要使用 tomcat 插件来部署。

关于如何配置的任何建议?

【问题讨论】:

  • 两场战争有什么不同?网页内容?
  • 请不要滥用个人资料。您应该有 1 个战争和单独的环境配置。另请参阅 stackoverflow.com/questions/18931181/… 这应该如何组合。

标签: java maven tomcat


【解决方案1】:

假设您有如下结构(仅作为示例)

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

您可以定义如下的程序集描述符:

<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>

此外,您还需要一个 maven-assembly-plugin 配置,如下所示:

<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>
  </executions>
</project>

如果您为每个环境添加一个单独的程序集描述符并添加一行描述符,您可以通过一次调用每个环境的包来创建。 这样做的一个缺点是您有几个汇编描述符,除了几行相同之外。

因此,您可以使用iterator-maven-plugin 来减少配置和维护的麻烦,如下所示:

<plugin>
  <groupId>com.soebes.maven.plugins</groupId>
  <artifactId>iterator-maven-plugin</artifactId>
  <version>0.3</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>iterator</goal>
      </goals>
      <configuration>
        <items>
          <item>test</item>
          <item>prod</item>
          <item>dev</item>
        </items>

        <pluginExecutors>
          <pluginExecutor>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.4</version>
            </plugin>
            <goal>single</goal>
            <configuration>
              <descriptors>
                <descriptor>src/assembly/iterator.xml</descriptor>
              </descriptors>
            </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

因此,您还需要一个合适的程序集描述符,如下所示:

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

  <id>${item}</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/${item}/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${project.build.directory}/environment/${item}/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

这使得维护这样的构建变得简单方便。完整的工作示例可以查看integration tests of iterator-maven-plugin

【讨论】:

  • 我不知道 Iterator 或 Assembly 插件,所以感谢您指出它们。但是,此配置采用在构建时为每个环境创建专用包的方法。这为编译时间和许多工件的存储在构建服务器上创建了很大的负载。我的计划是存储一个工件,即与环境无关的 WAR 文件。在部署时,我想修改工件以将其专门用于目标环境并进行部署。不保留此专用工件。
  • 无论如何,看来我应该将环境专业化保留在 Maven 之外。 Ant 似乎在构建后的步骤中很好地管理了这一点。
  • 你好@khmarbaise,我试试你的pom和程序集......但是在构建我的代码时,我收到了这个错误:Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (default-cli) on project Test: Error reading assemblies: No assembly descriptors found.
  • 首先你应该使用更新版本的 maven-assembly-plugin 其次你应该添加文件src/assembly/iterator.xml 如上所述...
猜你喜欢
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 2014-04-09
  • 2021-03-19
  • 1970-01-01
相关资源
最近更新 更多