كيفية إضفاء الطابع الخارجي على أجزاء من ملف بناء مخضرم؟

StackOverflow https://stackoverflow.com/questions/495208

سؤال

لقد واجهت مشكلة في كيفية إصدار ملف التكوين بتنسيق XML.أسهل طريقة هي كتابة تحديثات XSLT.يحتوي كل إصدار من التطبيق على تحديث XSLT خاص به.جميع ملفات التحديث هذه صغيرة بما يكفي ليتمكن 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.سيكون ملف pom.xml الخاص بي أكثر نظافة ومعلومات خارجية أسهل للصيانة.

كيف أقوم بذلك؟

هل يمكنني استخدام بعض بيانات التحكم التكرارية داخل ملف البناء؟هل هناك طريقة لاستيراد البيانات من بعض الملفات الخارجية؟

هل كانت مفيدة؟

المحلول

تسمح لك بعض المكونات الإضافية باستخدام واصفات خارجية (على سبيل المثال ملف maven-assembly-plugin).لسوء الحظ، فإن البرنامج المساعد xml-maven ليس واحدًا منهم بعد.

أحد الخيارات هو نسخ الأهداف ذات الصلة من البرنامج الإضافي xml-maven وإدخال المعالجة من 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) انسخ ملفات TransformMojo وAbstractXmlMojo .java من البرنامج المساعد xml-maven (تحتاج إلى mojo الأصل لترث الخصائص من javadoc الخاص به).

3) أضف أ واصفات الخاصية إلى TransformMojo:

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

4) قم بتعديل طريقة التنفيذ () لقراءة واصفات مجموعات التحويل

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) التنفيذ قراءة الواصفات () للسماح لك بتحديد موقع الواصفات على مسار الفصل أو داخل المشروع (يتم رفع معالجة القارئ إلى حد كبير من 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) التنفيذ الحصول على الموقع () لاستخدام استراتيجيات متنوعة لاكتشاف الملف إما كمسار نسبي أو عنوان 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) تجاوز كملف مطلق () لحل الملفات باستخدام استراتيجية محدد المواقع (يسمح لنا بتعريف ملفات 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) قم بإنشاء مشروع مخضرم جديد لاستضافة مجموعات التحويل الخاصة بك (على سبيل المثال يسمى xml-ext-test-descriptor).العملية هي نفسها بالنسبة ل واصفات مشتركة من البرنامج المساعد للتجميع، أي.أنشئ مشروعًا، وأضف بعض ملفات xml ضمن src/main/resources، ثم قم بتثبيت المشروع.ملفات XML هي من شكل التكوين القياسي لمجموعات التحويل.على سبيل المثال، ضع بعض التحويلات في 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>

إذا نجحت جميع الخطوات المذكورة أعلاه، فعند تنفيذها، سيعمل المكون الإضافي المخصص على حل ملف TransformSet1.xml وملفات xsl الخاصة بك من تبعية واصف اختبار xml-ext ومعالجتها كالمعتاد.

نصائح أخرى

وربما تكون هناك طرق أخرى للقيام بذلك، ولكن هل يمكن أن يكون قسم pluginManagement في بوم الأم.

<اقتباس فقرة>   

وpluginManagement: هو العنصر الذي ينظر إليه على طول الإضافات الجانبية. إدارة البرنامج المساعد يحتوي على عناصر المساعد بنفس الطريقة، إلا أنه بدلا من تكوين المساعد المعلومات لهذا بناء مشروع معين، فإنه يهدف إلى تكوين مشروع يبني التي ترث من هذا واحد. ومع ذلك، وهذا بتكوين فقط الإضافات التي يتم الرجوع إليها في الواقع داخل عنصر الإضافات في الأطفال. الأطفال لديهم كل الحق في تجاوز تعريفات pluginManagement.

وعلى سبيل المثال:

والمشروع الأصل POM (تحتاج إلى تشغيل mvn تثبيت لضمان هذا مرئيا لمشروع ابنك)

<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