首页 文章

根据输入参数选择性地复制maven中的资源?

提问于
浏览
0

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

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

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

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

此外,如果我使用此方法,目标war,将整个文件夹复制到它 . 如何配置maven只复制文件夹内容而不是整个文件夹 .

2 回答

  • 1

    复制资源时,Maven会将include / exclude模式应用于定义的 <directory> 值,并保持该目录中的路径结构 . 我找不到't any equivalent to ant' s flattenmapper .

    其余的,请考虑Maven build profiles . 您可以定义包含要在构建时更改的配置的配置文件,然后从命令行激活正确的配置文件 . 使用下面显示的配置,命令

    mvn -Dtarget.env=local process-resources

    将文件从 /src/main/resources/local 复制到 target/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>
    

    你可以用我在这里展示的配置文件做更多的事情 . 我希望这可以让你开始,即使它没有解决你的整个用例 .

  • 2

    我可以推荐的最好的事情是创建一个如下所示的结构(仅限于属性文件):

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

    结果将是通过单个构建两个war文件,这些文件已经配置用于 生产环境 ,另一个用于开发 .

相关问题