【问题标题】:Maven ressource filtering with M2E and external properties file使用 M2E 和外部属性文件过滤 Maven 资源
【发布时间】:2013-10-08 13:37:12
【问题描述】:

我有一个 Maven 项目,它读取一个外部属性文件来过滤资源。这在使用 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 执行单元测试时过滤资源。所以这仍然是开放的;)

【问题讨论】:

    标签: java maven m2eclipse


    【解决方案1】:

    当您说运行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>
    

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

    【讨论】:

    • 可能有点不清楚:如果我使用 Maven 构建/测试但不是从 IDE 构建/测试,测试运行良好
    【解决方案2】:

    尝试在如下块中包含生命周期映射插件。我已经使用其他没有 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 中有一个绿色构建,因此请尝试将配置中的 &lt;execute/&gt; 标记替换为:

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

    然后做一个 Maven --> 再次更新项目。

    Eclipse Wiki page on the topic

    【讨论】:

    • 感谢您的回答,但我真的不知道该做什么;)。我复制了 ... 部分,现在我得到了一个 NPE,而 maven 在 eclipse 中更新了项目。只需复制粘贴您的东西就不会发生任何事情(我认为这是正确的;))您能否向我提供有关此的更多信息?
    • 啊,好吧,我只是弄错了...这实际上使它变绿了,但在生命周期中,但它仍然无法正常工作;)
    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 2015-10-15
    • 2015-06-10
    • 2012-04-05
    • 1970-01-01
    • 2016-02-14
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多