Frage

Ich wurde zu einem Problem konfrontiert, wie eine Konfigurationsdatei Version im XML-Format. Der einfachste Weg ist, um XSLT-Updates zu schreiben. Jede Version der Anwendung hat ihr eigenes XSLT-Update. All diese Update-Dateien sind klein genug, um managable von der IDE, vor allem der DIFF Werkzeug.

Da das Projekt bereits als Maven2 Java logische Lösung entwickelt worden ist, war diesen Updates durch Maven-Build-Datei zu starten.

Dies ist, wie der Abschnitt eine Reihe von neuen Updates sucht heute für die Anwendung:

<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>

Ich möchte Informationen über transformationSet in einigen Eigenschaften / XML-Datei importieren externalisieren. Meine pom.xml Datei wird sauberer und externalisierte Informationen leichter Wartung.

Wie kann ich das tun?

Kann ich etwas Iterieren Steueranweisung innerhalb der Build-Datei? Gibt es eine Möglichkeit, Daten aus einer externen Datei zu importieren?

War es hilfreich?

Lösung

Einige Plugins können Sie externe Deskriptoren verwenden (zum Beispiel die maven-Montage-Plugin ). Leider ist die xml-Maven-Plugin ist noch nicht einer von ihnen.

Eine Möglichkeit ist, die relevanten Ziele aus der XML-Maven-Plugin und Schuhanzieher die Verarbeitung von Maven-shared-io in das Ziel zu kopieren. Ich habe mich das selbst mit einem Blick zu tun Anfragen gegen verschiedene Plugins zu erhöhen Descriptor-Dateien und den LocatorStrategy Ansatz zu verwenden, um die Deskriptoren zu finden. Hier ist ein Teil der Verarbeitung, die die XML-Maven-Plugin ändern, um Deskriptoren zu erlauben zu nutzen. Hinweis gibt es wenig Validierung der Dateien, so dass es ein bisschen zerbrechlich ist wie es ist, aber es funktioniert.

1) Erstellen Sie ein neues Maven-Plugin-Projekt (sagen genannt xml-ext-Maven-Plugin) mit folgenden Abhängigkeiten:

<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) Kopieren Sie die TransformMojo und die AbstractXmlMojo .java-Dateien aus der XML-Maven-Plugin (Sie müssen die übergeordneten mojo die Eigenschaften von seiner javadoc zu erben).

3) Fügen Sie ein Deskriptoren Immobilien zum TransformMojo:

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

4) modifizieren, um die Methode execute () die Deskriptoren für transformationSets lesen

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) Implementieren readDescriptors () , damit Sie Deskriptoren auf dem Classpath oder im Rahmen des Projektes finden (der Leser Verarbeitung wird von der Montage-Plugins DefaultAssemblyReader weitgehend aufgehoben). Hinweis gibt es wenig Validierung in dieser Implementierung würde eine richtige Plugin prüfen, ob Werte usw. eingestellt.

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) Implementieren Sie getLocation () verschiedene Strategien zu verwenden, um die Datei entweder als relative Pfad, URL oder aus dem Classpath zu entdecken.

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) außer Kraft setzen asAbsoluteFile () zu lösen Dateien der Locator-Strategie (ermöglicht es uns, die XSL-Dateien in dem Descriptor Projekt zu definieren als auch).

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) Installieren Sie das Plugin auf Ihrem Repository.

9) Erstellen Sie ein neues Maven Projekt Ihre transformationSets Host (sagen xml-ext-Test-Beschreiber) genannt. das Verfahren ist das gleiche wie für die gemeinsamen Deskriptoren die Assembly-Plugin, erstellen also ein Projekt, einige xML-Dateien unter src / main / resources hinzufügen und das Projekt installieren. Die XML-Dateien sind von der Form der Standard transformationSets Konfiguration. Zum Beispiel ein paar der Transformationen in src setzen / 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) Setzen Sie Ihre XSL-Dateien in dem Descriptor Projekt, z.B. src / main / resources / Stylesheets / update1-8-3.xsl

11) Konfigurieren Sie das neue Plugin in Ihrem Projekt das Descriptor Projekt als Abhängigkeit zu verweisen und die XML-Datei als Deskriptor verweisen:

<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>

Wenn alle oben genannten Schritte gearbeitet, wenn sie ausgeführt wird das benutzerdefinierte Plugin transformationSet1.xml und Ihre XSL-Dateien aus der XML-ext-Test-Descriptor Abhängigkeit lösen wird und sie als normale verarbeiten.

Andere Tipps

Es kann auch andere Wege, dies zu tun, aber man konnte einen pluginManagement Abschnitt in ein Elternteil pom.

  

pluginManagement: ist ein Element, das an der Seite von Plugins zu sehen ist. Plugin-Management enthält Elemente Plugin in der gleichen Art und Weise, mit der Ausnahme, dass anstatt der Konfiguration Informationen Plugin für dieses Projekt zu bauen, ist es beabsichtigt, konfigurieren Projekt erstellt, die von diesem erben. Dies ist jedoch nur konfiguriert Plugins, die tatsächlich referenziert werden innerhalb des Plugins Element in den Kindern. Die Kinder haben jedes Recht pluginManagement Definitionen außer Kraft zu setzen.

Zum Beispiel:

übergeordnetes Projekt POM (müssen mvn installieren, um sicherzustellen, das ist sichtbar, um Ihr Kind Projekt auszuführen)

<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>

Child Projekt 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>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top