代码之家  ›  专栏  ›  技术社区  ›  Boris Pavlović

如何将maven构建文件的片段外部化?

  •  2
  • Boris Pavlović  · 技术社区  · 16 年前

    我遇到了一个问题,如何以XML格式版本配置文件。最简单的方法是编写XSLT更新。应用程序的每个版本都有自己的XSLT更新。所有这些更新文件都很小,足以由IDE管理,尤其是 微分 工具。

    由于项目已经被开发为Mave2 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文件将更干净,外部化信息更容易维护。

    我该怎么做?

    我可以在构建文件中使用一些迭代控制语句吗?有没有从外部文件导入数据的方法?

    2 回复  |  直到 15 年前
        1
  •  3
  •   Rich Seller    15 年前

    一些插件允许您使用外部描述符(例如 maven-assembly-plugin )不幸的是,XML Maven插件还不是其中之一。

    一种选择是从XML Maven插件复制相关的目标,并将Maven共享IO的处理推送到目标中。我一直在寻找自己这样做的目的是针对各种插件提出使用描述符文件和locatorstrategy方法查找描述符的请求。下面是一些将修改XML Maven插件以允许使用描述符的处理。注意,所涉及的文件几乎没有经过验证,因此它有点脆弱,但确实有效。

    1)创建一个新的maven插件项目(称为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插件复制TrimeMojo和ActudixXMLJJO.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)实施 读描述符() 允许您在类路径或项目中定位描述符(读卡器处理主要从程序集插件的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)重写 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测试描述符)。过程与 shared descriptors 在程序集插件中,即创建一个项目,在src/main/resources下添加一些XML文件,然后安装该项目。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>
    

    如果上述所有步骤都有效,则在执行时,自定义插件将从XML ext测试描述符依赖项中解析transformationset1.xml和XSL文件,并按正常方式处理它们。

        2
  •  1
  •   toolkit    16 年前

    可能还有其他方法,但你可以 pluginManagement 父POM中的节。

    PluginManagement:是一个沿着侧边插件看到的元素。插件管理以几乎相同的方式包含插件元素,除了不为此特定项目生成配置插件信息之外,它旨在配置继承自此项目生成的项目生成。但是,这只配置在子级的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>