0

我正在将应用程序从 Ant 迁移到 Maven。有两种环境,这个应用程序可以部署到。一种配置称为 Local,另一种称为 dev,因此自然有两个 ant 目标。每个 ant 目标,从两个不同的文件夹复制资源,并使其成为最终战争文件的一部分。

我如何在 Maven 中模仿这种行为?我正在考虑在构建时传递一个参数,并基于这个参数,我们可以使用 maven 资源插件来复制相应的文件夹。像这样的东西。

<resources>
          <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>${local}/**</exclude>
                </excludes>
            </resource>
        </resources>

我在正确的道路上吗?如果是,我如何在运行时替换“本地”?另外,我正在做一个补充操作,如果我们想为 Dev 构建,那么我排除本地,反之亦然。有没有更好的方法来处理这个?

此外,如果我使用此方法,目标战争会将整个文件夹复制到其中。如何将 Maven 配置为仅复制文件夹内容而不是整个文件夹。

4

2 回答 2

2

复制资源时,Maven 会将包含/排除模式应用于<directory>定义的值,并维护从该目录向下的路径结构。我找不到任何与蚂蚁相同的东西flattenmapper

其余的,请考虑Maven 构建配置文件。您定义包含要在构建时更改的配置的配置文件,然后从命令行激活正确的配置文件。使用如下所示的配置,命令

mvn -Dtarget.env=local process-resources

会将文件从复制/src/main/resources/localtarget/classes. 由于 'local' 包含在资源的directory元素中,因此它不会出现在复制资源的路径中。

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <property>
                <name>target.env</name>
                <value>local</value>
            </property>
        </activation>
        <properties>
            <resource.dir>local</resource.dir>
        </properties>
    </profile>

    <!-- add a similar profile for dev -->
</profiles>

<resources>
    <resource>
        <directory>src/main/resources/${resource.dir}</directory>
        <filtering>true</filtering>
    </resource>
</resources>

您可以使用我在此处显示的配置文件做更多事情。我希望这能让你开始,即使它不能解决你的整个用例。

于 2012-06-27T20:43:52.717 回答
1

我可以推荐的最好的事情是创建如下结构(仅使用属性文件):

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

创建一个如下所示的程序集描述符,以与每个环境的 maven-assembly-plugin 结合使用:

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

最后,您需要为每个环境执行一次:

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

结果将是通过单个构建获得两个已配置用于生产和另一个用于开发的战争文件。

于 2012-06-27T20:43:04.303 回答