質問

XML 形式で構成ファイルをバージョン管理する方法が問題に直面しました。最も簡単な方法は、XSLT 更新を作成することです。アプリケーションの各リリースには独自の XSLT アップデートがあります。これらの更新ファイルはすべて、IDE、特に IDE で管理できるほど小さいです。 差分 道具。

プロジェクトはすでに Maven2 Java として開発されているため、論理的な解決策は Maven ビルド ファイルを通じてこれらの更新をトリガーすることでした。

現在、一連の更新プログラムを適用するセクションは次のようになります。

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>xml-maven-plugin</artifactId>
  <executions>
    <execution>
    <phase>compile</phase>
      <goals>
        <goal>transform</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <transformationSets>
      <transformationSet>
        <dir>config/xsltUpdates/input</dir>
        <stylesheet>config/xsltUpdates/update1-8-3.xsl</stylesheet>
        <outputDir>config/xsltUpdates/update1-8-3</outputDir>
      </transformationSet>
      <transformationSet>
         <dir>config/xsltUpdates/update1-8-3</dir>
         <stylesheet>config/xsltUpdates/update1-8-9.xsl</stylesheet>
         <outputDir>config/xsltUpdates/update1-8-9</outputDir>
      </transformationSet>
      <transformationSet>
        <dir>config/xsltUpdates/update1-8-9</dir>
        <stylesheet>config/xsltUpdates/update1-9-0.xsl</stylesheet>
        <outputDir>config/xsltUpdates/update1-9-0</outputDir>
      </transformationSet>
      <transformationSet>
        <dir>config/xsltUpdates/update1-9-0</dir>
        <stylesheet>config/xsltUpdates/update1-10-0.xsl</stylesheet>
        <outputDir>config/xsltUpdates/update1-10-0</outputDir>
      </transformationSet>
      <transformationSet>
        <dir>config/xsltUpdates/update1-10-0</dir>
        <stylesheet>config/xsltUpdates/update1-10-0-1.xsl</stylesheet>
        <outputDir>config/xsltUpdates/update1-10-0-1</outputDir>
      </transformationSet>
      <transformationSet>
        <dir>config/xsltUpdates/update1-10-0-1</dir>
        <stylesheet>config/xsltUpdates/update1-10-0-2.xsl</stylesheet>
        <outputDir>config/xsltUpdates/update1-10-0-2</outputDir>
      </transformationSet>
      <transformationSet>
        <dir>config/xsltUpdates/update1-10-0-2</dir>
        <stylesheet>config/xsltUpdates/updateCurrent.xsl</stylesheet>
        <outputDir>config/xsltUpdates/output</outputDir>
      </transformationSet>
    </transformationSets>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>net.sf.saxon</groupId>
      <artifactId>saxon</artifactId>
      <version>8.7</version>
    </dependency>
  </dependencies>
</plugin>

いくつかのプロパティ/xml ファイルのインポートで、transformationSet に関する情報を外部化したいと考えています。pom.xml ファイルはよりクリーンになり、情報が外部化され、メンテナンスが容易になります。

どうやってやるの?

ビルド ファイル内で反復制御ステートメントを使用できますか?外部ファイルからデータをインポートする方法はありますか?

役に立ちましたか?

解決

一部のプラグインでは、外部記述子を使用できます (たとえば、 maven-アセンブリ-プラグイン)。残念ながら、xml-maven-plugin はまだその 1 つではありません。

1 つのオプションは、関連するゴールを xml-maven-plugin からコピーし、maven-shared-io からの処理をゴールに組み込むことです。私は、記述子ファイルと LocatorStrategy アプローチを使用して記述子を見つけるためのさまざまなプラグインに対するリクエストを提起することを目的として、これを自分で行うことを検討してきました。ここでは、記述子を使用できるように xml-maven-plugin を変更する処理をいくつか示します。関連するファイルの検証がほとんど行われていないため、このままでは少し脆弱ですが、動作することに注意してください。

1) 次の依存関係を持つ新しい maven-plugin プロジェクト (xml-ext-maven-plugin という名前) を作成します。

<dependencies>
  <dependency>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xml-maven-plugin</artifactId>
    <version>1.0-beta-2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-2</version>
  </dependency>
</dependencies>

2) xml-maven-plugin から TransformMojo および AbstractXmlMojo .java ファイルをコピーします (親 Mojo が javadoc からプロパティを継承する必要があります)。

3) を追加します 記述子 プロパティを TransformMojo に追加します。

/**
 * A list of descriptor files to obtain the transformation sets from
 * 
 * @parameter
 */
private String[] descriptors;

4) 変換セットの記述子を読み取るようにexecute()メソッドを変更します。

public void execute() throws MojoExecutionException, MojoFailureException {
    //insert at start of method to resolve transformationSets fronm descriptors
    if (descriptors != null && descriptors.length > 0) {
        transformationSets = readDescriptors(descriptors);
    }

    ...

5) 実装する readDescriptors() これにより、クラスパス上またはプロジェクト内で記述子を見つけることができるようになります (リーダーの処理は、アセンブリ プラグインの DefaultAssemblyReader から大幅に引き継がれます)。この実装には検証がほとんどないことに注意してください。適切なプラグインは、値が設定されているかどうかなどをチェックします。

private TransformationSet[] readDescriptors(String[] descriptors)
        throws MojoExecutionException {

    List descriptorSets = new ArrayList();
    // add all the existing transformationSets
    if (transformationSets != null && transformationSets.length != 0) {
        descriptorSets.addAll(Arrays.asList(transformationSets));
    }

    for (int i = 0; i < descriptors.length; i++) {
        getLog().info(
                "Reading transformation descriptor: " + descriptors[i]);

        Location location = getLocation(descriptors[i]);

        Reader reader = null;
        try {
            reader = new InputStreamReader(location.getInputStream(),
                    "UTF-8");

            Xpp3Dom dom = Xpp3DomBuilder.build(reader);

            descriptorSets.addAll(parseTransformationSets(dom));
        } catch (IOException e) {
            throw new MojoExecutionException(
                    "Error reading transformation descriptor: "
                            + descriptors[i], e);
        } catch (XmlPullParserException e) {
            throw new MojoExecutionException(
                    "Error parsing transformation descriptor: "
                            + descriptors[i], e);
        } finally {
            IOUtil.close(reader);
        }
    }

    return (TransformationSet[]) descriptorSets
            .toArray(new TransformationSet[descriptorSets.size()]);
}

/**
 * Create transformationSets from the Xpp3Dom.
 * TODO use plexus utilities to resolve these elegantly?
 */
private List parseTransformationSets(Xpp3Dom dom) {
    // TODO validation of the input files!
    Xpp3Dom[] setDoms = dom.getChildren("transformationSet");

    List sets = new ArrayList();
    for (int i = 0; i < setDoms.length; i++) {
        TransformationSet set = new TransformationSet();
        set.setDir(new File(setDoms[i].getChild("dir").getValue()));
        set.setStylesheet(new File(setDoms[i].getChild("stylesheet")
                .getValue()));

        Xpp3Dom outDom = setDoms[i].getChild("outputDir");

        if (outDom != null) {
            set.setOutputDir(new File(outDom.getValue()));
        }

        sets.add(set);
    }
    return sets;
}

6) 実装する getLocation() さまざまな戦略を使用して、相対パス、URL、またはクラスパスのいずれかとしてファイルを検出します。

private Location getLocation(String path) {
    List strategies = new ArrayList();
    strategies.add(new RelativeFileLocatorStrategy(getBasedir()));
    strategies.add(new ClasspathResourceLocatorStrategy());
    strategies.add(new FileLocatorStrategy());
    strategies.add(new URLLocatorStrategy());

    List refStrategies = new ArrayList();
    refStrategies.add(classpathStrategy);

    Locator locator = new Locator();

    locator.setStrategies(strategies);

    Location location = locator.resolve(path);
    return location;
}

7) オーバーライド asAbsoluteFile() ロケーター戦略を使用してファイルを解決します (記述子プロジェクトで xsl ファイルを定義することもできます)。

protected File asAbsoluteFile(File f) {
    String path = f.getPath();

    // ensure we're getting a path in the form that URL can handle
    if (path != null) {
        path = path.replaceAll("\\\\", "/");
    }
    Location location = getLocation(path);

    if (location == null) {
        //can't find the file, let the parent implementation have a try
        return super.asAbsoluteFile(f);
    }
    try {
        return location.getFile();
    } catch (IOException e) {
        throw new RuntimeException("unable to read file " + f.getPath(), e);
    }
}

8) プラグインをリポジトリにインストールします。

9) 変換セットをホストする新しい Maven プロジェクトを作成します (xml-ext-test-descriptor という名前)。プロセスは 共有記述子 アセンブリプラグインの、つまりプロジェクトを作成し、src/main/resources にいくつかの XML ファイルを追加して、プロジェクトをインストールします。XML ファイルは、標準のtransformationSets構成の形式です。たとえば、いくつかの変換を src/main/resources/transformations1.xml に配置します。

<transformationSets>
  <transformationSet>
    <!--the config directory is in the root of the project -->
    <dir>config/xsltUpdates/input</dir>
    <!-- the stylesheet can be in the descriptor project-->
    <stylesheet>/stylesheets/update1-8-3.xsl</stylesheet>       
    <outputDir>config/xsltUpdates/update1-8-3</outputDir>
  </transformationSet>
  <transformationSet>
     <dir>config/xsltUpdates/update1-8-3</dir>
     <stylesheet>/stylesheets/update1-8-9.xsl</stylesheet>
     <outputDir>config/xsltUpdates/update1-8-9</outputDir>
  </transformationSet>
</transformationSets>

10) xsl ファイルを記述子プロジェクトに配置します。src/main/resources/stylesheets/update1-8-3.xsl

11) 記述子プロジェクトを依存関係として参照し、xml ファイルを記述子として参照するように、プロジェクト内の新しいプラグインを構成します。

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>xml-maven-plugin</artifactId>
  <executions>
    <execution>
      <phase>compile</phase>
      <goals>
        <goal>transform</goal>
      </goals>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>name.seller.rich</groupId>
      <artifactId>xml-ext-test-descriptor</artifactId>
      <version>0.0.1</version>
    </dependency>
  </dependencies>
  <configuration>
   <descriptors>
     <!-- will be resolved from xml-ext-test-descriptor -->
     <descriptor>/transformationSet1.xml</descriptor>
   </descriptors>
 </plugin>

上記の手順がすべて成功した場合、カスタム プラグインを実行すると、transformationSet1.xml と xsl ファイルが xml-ext-test-descriptor 依存関係から解決され、通常どおり処理されます。

他のヒント

これを行うには他の方法があるかもしれませんが、 pluginManagement 親 pom 内のセクション。

プラグイン管理:はプラグインと一緒に見られる要素です。プラグイン管理には、この特定のプロジェクト ビルドのプラグイン情報を構成するのではなく、このプロジェクト ビルドから継承するプロジェクト ビルドを構成することを目的としている点を除いて、ほぼ同じ方法でプラグイン要素が含まれています。ただし、これにより構成されるのは、子の plugins 要素内で実際に参照されるプラグインのみです。子には、pluginManagement 定義をオーバーライドするあらゆる権利があります。

例えば:

親プロジェクト POM (これが子プロジェクトに表示されることを確認するには、mvn install を実行する必要があります)

<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>org.nkl</groupId>
  <artifactId>parent</artifactId>
  <packaging>pom</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>xml-maven-plugin</artifactId>
          <executions>
            <execution>
              <phase>compile</phase>
              <goals>
                <goal>transform</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <transformationSets>
              <transformationSet>
                <dir>config/xsltUpdates/input</dir>
                <stylesheet>config/xsltUpdates/update1-8-3.xsl</stylesheet>
                <outputDir>config/xsltUpdates/update1-8-3</outputDir>
              </transformationSet>
              <transformationSet>
                <dir>config/xsltUpdates/update1-8-3</dir>
                <stylesheet>config/xsltUpdates/update1-8-9.xsl</stylesheet>
                <outputDir>config/xsltUpdates/update1-8-9</outputDir>
              </transformationSet>
              <transformationSet>
                <dir>config/xsltUpdates/update1-8-9</dir>
                <stylesheet>config/xsltUpdates/update1-9-0.xsl</stylesheet>
                <outputDir>config/xsltUpdates/update1-9-0</outputDir>
              </transformationSet>
              <transformationSet>
                <dir>config/xsltUpdates/update1-9-0</dir>
                <stylesheet>config/xsltUpdates/update1-10-0.xsl</stylesheet>
                <outputDir>config/xsltUpdates/update1-10-0</outputDir>
              </transformationSet>
              <transformationSet>
                <dir>config/xsltUpdates/update1-10-0</dir>
                <stylesheet>config/xsltUpdates/update1-10-0-1.xsl</stylesheet>
                <outputDir>config/xsltUpdates/update1-10-0-1</outputDir>
              </transformationSet>
              <transformationSet>
                <dir>config/xsltUpdates/update1-10-0-1</dir>
                <stylesheet>config/xsltUpdates/update1-10-0-2.xsl</stylesheet>
                <outputDir>config/xsltUpdates/update1-10-0-2</outputDir>
              </transformationSet>
              <transformationSet>
                <dir>config/xsltUpdates/update1-10-0-2</dir>
                <stylesheet>config/xsltUpdates/updateCurrent.xsl</stylesheet>
                <outputDir>config/xsltUpdates/output</outputDir>
              </transformationSet>
            </transformationSets>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>net.sf.saxon</groupId>
              <artifactId>saxon</artifactId>
              <version>8.7</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

子プロジェクト POM

<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">
  <parent>
    <artifactId>parent</artifactId>
    <groupId>org.nkl</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.nkl</groupId>
  <artifactId>child</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>xml-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top