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

{username_timestamp} を計算する Java コードがあり、ライフサイクルの後のステップで使用するために、そのコードが計算された {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 に答える