我是Maven的新手,我的项目最终正在编译并正确运行。
在每次运行中,我的项目会在运行时创建的动态位置(username_timestamp
)上编写报告,并使用此位置设置名为System.property
的{{1}}。执行后,我想使用maven目标将一些静态资源(样式,图像,js等)复制到此动态文件夹。
我无法弄清楚如何让Maven了解这个动态位置或访问此System.property
我准备好让我的项目将这些资源复制到目录中,但我想我会再试一次,以防有一种简单/ Maven的方法。
我已经将资源复制到硬编码位置了。这是POM的片段。我正在使用Jbehave的Maven目标,他们确实按顺序执行
REPORTS_LOCATION
答案 0 :(得分:3)
听起来你有一段计算{username_timestamp}的Java代码,然后你希望这些代码能够将计算出的{username_timestamp}传回Maven,以便在其生命周期的后续步骤中使用。我不确定这是可能的。相反,如何反转过程,以便Maven生成时间戳,并从代码中消耗它?您可以使用build-helper-maven-plugin,Maven资源过滤和Java代码的组合来加载属性文件。
<?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>
timestamp=${timestamp}
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"));
}
}
答案 1 :(得分: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>
答案 2 :(得分:0)
如果在运行时将文件夹设置为环境变量,则可以执行以下操作:
只需使用
<properties>
<REPORTS_LOCATION><${env.REPORTS_LOCATION}></REPORTS_LOCATION>
</properties>
然后你可以通过你的pom
中的$ {REPORTS_LOCATION}来引用这个属性