2

我是一个 Maven 新手,我的项目终于可以正确编译和运行了。

在每次运行时,我的项目都会在运行时创建的动态位置 ( ) 上编写报告,username_timestamp并使用该位置设置一个System.property调用REPORTS_LOCATION。执行后,我想使用 maven 目标将一些静态资源(样式、图像、js 等)复制到这个动态文件夹中。

我想不通的是如何让 Maven 知道这个动态位置或访问这个 System.property

我准备让我的项目将这些资源复制到目录中,但我想我会再试一次,以防有一种简单的/Maven 方式来执行此操作。

我已经将资源复制到硬编码位置。这是 POM 的一个片段。我正在使用 Jbehave 的 Maven 目标,它们确实按顺序执行

<plugins>
  <plugin>
    <groupId>org.jbehave</groupId>
    <artifactId>jbehave-maven-plugin</artifactId>
    <version>${jbehave.core.version}</version>
    <executions>
      <execution>
        <id>embeddable-stories</id>
        <phase>integration-test</phase>
        <configuration>
          <includes>
            <include>**/Stories.java</include>
          </includes>
          <excludes />
          <metaFilters>
            <metaFilter>${meta.filter}</metaFilter>
          </metaFilters>
        </configuration>
        <goals>
          <goal>run-stories-as-embeddables</goal>
        </goals>
      </execution>
      <!-- Copy the resources AFTER the execution is done -->
      <execution>
        <id>unpack-view-resources</id>
        <phase>integration-test</phase>
        <configuration>
            <viewDirectory>${basedir}/src/main/java/project/reports/{NEED TO FEED DIRECTORY HERE}</viewDirectory>
        </configuration>
        <goals>
          <goal>unpack-view-resources</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
4

3 回答 3

3

听起来您有一段 Java 代码计算 {username_timestamp},然后您希望该代码能够将计算出的 {username_timestamp} 传送回 Maven,以便在其生命周期的后续步骤中使用。我不确定这是可能的。相反,如何反转过程,以便 Maven 生成时间戳,然后您从代码中使用它?您可以结合使用 build-helper-maven-plugin、Maven 资源过滤和 Java 代码来加载属性文件来实现此目的。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>junk</groupId>
    <artifactId>junk</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <!--
            Use build-helper-maven-plugin to generate a timestamp during the
            initialize phase and store it as a property named "timestamp".
            -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>timestamp-property</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>timestamp-property</goal>
                        </goals>
                        <configuration>
                            <locale>en_US</locale>
                            <name>timestamp</name>
                            <pattern>yyyyMMDDHHmmssSSS</pattern>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>

                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>

                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>

                    <finalName>${pom.artifactId}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>

                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <!--
        Turn on resource filtering so that references to ${timestamp} in a
        properties file get replaced with the value of the timestamp property.
        -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

src/main/resources/junk.properties

timestamp=${timestamp}

src/main/java/Main.java

import java.util.Properties;

public final class Main {

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.load(Main.class.getResourceAsStream("/junk.properties"));
        System.out.println(props.getProperty("timestamp"));
    }
}
于 2012-07-14T06:54:52.127 回答
1

非常感谢 cnauroth,这就像一个魅力。这是我工作更新的 POM,以防它帮助其他人

<resources>
  <resource>
    <directory>${basedir}/src/main/java/resources</directory>
    <excludes><exclude>**/locale/**</exclude></excludes>
    <filtering>true</filtering>
  </resource>
</resources>
<plugins>

<!-- Use build-helper-maven-plugin to generate a timestamp during the initialize 
    phase and store it as a property named "mavenTimestamp". -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>timestamp-property</id>
            <phase>initialize</phase>
            <goals>
                <goal>timestamp-property</goal>
            </goals>
            <configuration>
                <locale>en_US</locale>
                <name>mavenTimestamp</name>
                <pattern>yyyyMMDDHHmmssSSS</pattern>
            </configuration>
        </execution>
    </executions>
</plugin>

  <plugin>
    <groupId>org.jbehave</groupId>
    <artifactId>jbehave-maven-plugin</artifactId>
    <version>${jbehave.core.version}</version>
    <executions>
      <execution>
        <id>embeddable-stories</id>
        <phase>integration-test</phase>
        <configuration>
          <includes>
            <include>**/Stories.java</include>
          </includes>
          <excludes />
          <ignoreFailureInStories>true</ignoreFailureInStories>
          <verboseFailures>true</verboseFailures>
          <threads>5</threads>
          <metaFilters>
            <metaFilter>${meta.filter}</metaFilter>
          </metaFilters>
        </configuration>
        <goals>
          <goal>run-stories-as-embeddables</goal>
        </goals>
      </execution>
      <!-- THIS WORKS :) Copy the resources AFTER the execution is done -->
       <execution>
        <id>unpack-view-resources</id>
        <phase>integration-test</phase>
        <configuration>
            <viewDirectory>${basedir}/src/main/java/project/reports/${mavenTimestamp}</viewDirectory>
        </configuration>
        <goals>
          <goal>unpack-view-resources</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

于 2012-07-16T17:35:11.927 回答
0

如果在运行时将文件夹设置为环境变量,则可以执行以下操作:

只需使用

    <properties>
        <REPORTS_LOCATION><${env.REPORTS_LOCATION}></REPORTS_LOCATION>
    </properties>

然后您可以通过 pom 中的 ${REPORTS_LOCATION} 引用该属性

于 2013-10-16T17:20:34.357 回答