使用M2E和外部属性文件进行Maven资源过滤

时间:2013-10-08 13:37:13

标签: java maven m2eclipse

我有一个maven projekt,它读取外部属性文件来过滤资源。这在使用mvn包时工作正常但是从JUnit测试开始这只有在pom本身而不是属性文件中声明属性时才有效,所以我认为插件配置是问题所在。我在我的pom中得到了这个:

<plugin>
    <groupId>org.eclipse.m2e</groupId>
    <artifactId>lifecycle-mapping</artifactId>
    <version>1.0.0</version>
    <configuration>
      <lifecycleMappingMetadata>
        <pluginExecutions>
          <pluginExecution>
            <pluginExecutionFilter>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>properties-maven-plugin</artifactId>
              <versionRange>[1.0-alpha-2,)</versionRange>
              <goals>
                <goal>read-project-properties</goal>
              </goals>
            </pluginExecutionFilter>
            <action>
              <execute />
            </action>
          </pluginExecution>
        </pluginExecutions>
      </lifecycleMappingMetadata>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
      <execution>
        <phase>initialize</phase>
        <goals>
          <goal>read-project-properties</goal>
        </goals>
        <configuration>
          <files>
            <file>${build.properties.file}</file>
          </files>
        </configuration>
      </execution>
    </executions>
  </plugin>

生命周期在Eclipse中标记为红色。

编辑:

m2e部分需要被插件管理包围,而错误消失了。我可以看到目前正在Maven偏好下执行的目标。

但是如果我使用这个插件,它实际上仍然没有在从eclipse执行单元测试时过滤ressources。所以这仍然是开放的;)

2 个答案:

答案 0 :(得分:0)

当你说运行mvn package时它运行良好,因为包运行测试用例时,我不是很清楚。 所以我的假设可能是通过跳过测试来正确运行包。 为了将属性传递给测试用例,您可以使用surefire插件,如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <systemProperties>
        <property>
          <name>filePath</name>
          <value>/path/to/the/file</value>
        </property>
      </systemProperties>
    </configuration>
  </plugin>

这将选择属性并传递给测试用例。

答案 1 :(得分:0)

尝试在以下块中包含生命周期映射插件。我已经使用其他没有Eclipse连接器的Maven插件完成了这项工作。

    <profile>
      <!-- Contents of this profile are only needed to make this project work in       
           Eclipse. Once all appropriate m2e connectors are available, this can be
           removed -->
        <id>m2e</id>
        <activation>
            <property>
                <name>m2e.version</name>
            </property>
        </activation>
        <build>
          <pluginManagement>
            <plugins>
              <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <!-- other plugin config here ->
              </plugin>
            </plugins>
          </pluginManagement>
        </build>
      </profile>

编辑:好的,你现在在Eclipse中有一个绿色版本,所以尝试用这个替换配置中的<execute/>标签:

<execute>
    <runOnIncremental>true</runOnIncremental>
    <runOnConfiguration>true</runOnConfiguration>
</execute>

然后做一个Maven - &gt;再次更新项目。

Eclipse Wiki page on the topic