【问题标题】:Integrating ant resource generation targets into a Maven build将 ant 资源生成目标集成到 Maven 构建中
【发布时间】:2011-06-01 11:46:28
【问题描述】:

我目前正在处理一个相当大的项目,该项目已从 Ant 迁移到 Maven。实际构建过程没有问题(它可以很好地编译和打包源代码)。

问题是我还有很多目标可以为项目生成额外的资源(编译 LessCSS、生成和上传文档、为自定义标签和函数生成 tld 文件等)。我不确定我应该如何处理这些任务。我们以构建 CSS&JS 的目标为例(其他的或多或少相似,但没有联系)。它看起来像这样(简化):

<target name="build.css_js">
    <concat destfile="${webapp.dir}/scripts/main.js">
        <fileset dir="${webapp.dir}/scripts/src" includes="*.js"/>
    </concat>

    <!-- build css files from less sources -->
    <taskdef name="lesscss" classname="com.asual.lesscss.LessEngineTask" classpathref="libraries" />
    <lesscss input="${webapp.dir}/syles/src/input.less" output="${webapp.dir}/styles/output.css" />
</target>

pom.xml 我设置了以下插件:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <configuration>
               <tasks>
                   <echo message="Hello World from pom.xml"/>
                   <ant target="build.css_js"/>
               </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我正在使用的依赖项不再在我们的 SVN 存储库中(因为它们由 Maven 管理),所以我将库变量切换为指向 Maven 存储库:

<property name="lib.dir" location="${env.HOMEPATH}/.m2/repository" />

这不好,因为该路径可能仅在我的机器上有效。我不知道有任何其他方法可以从 Maven 存储库中引用这些库,我需要它们来运行 ant 目标。

  • 我的方法可以吗,还是有更好的做事方式?
  • 如何解决图书馆问题?
  • 打包项目时需要一些资源,但有些不需要。 是否存在超出编译/打包范围的生命周期阶段?我找到了我认为符合我需求的site 生命周期。
  • 理想情况下,我应该完全放弃 ant 构建文件,但我不确定让脚本作为 maven 插件运行是否值得(我目前不知道如何做到这一点)。您对此有何看法?

我是 Maven 新手,因此欢迎提出任何建议。

【问题讨论】:

    标签: maven ant maven-antrun-plugin


    【解决方案1】:

    通常嵌入 antrun 调用并不理想,但如果您没有找到合适的插件来做您需要的事情,那么我不会担心。如果处理相当简单,实际上很容易将其嵌入到自己的 Maven 插件中,请参阅this example 以获取入门帮助。

    如果您使用 antrun,并且依赖项 jar 已经安装到您的 Maven 存储库,您可以通过将 antrun 插件添加为插件配置的依赖项来配置 antrun 插件以在其执行中使用这些 jar。这意味着依赖项将被解析并可供使用,但对您的项目不可见(有助于避免意外包含)。然后以便携式方式访问它们,您可以使用:

    <property name="lib.dir" location="${settings.localRepository}" />
    

    或者,您可以使用其他一些可用属性将 Maven 类路径公开给 antrun 插件,例如 ${maven.compile.classpath} 请参阅 antrun documentation 了解更多详细信息。

    如果你有多个 ant 的离散执行,你可以在 antrun 插件中单独配置它们,并为每个指定一个合适的id。下面的示例显示了两个执行,都绑定到 process-resources 阶段。当然你需要提供一些实际的目标。

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <executions>
        <execution>
          <id>build-css</id>
          <phase>generate-resource</phase>
          <configuration>
            <target>
              ...
            </target>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
        <execution>
          <id>build-js</id>
          <phase>generate-resource</phase>
          <configuration>
            <target>
              ...
            </target>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>some.group.id</groupId>
          <artifactId>artifactId</artifactId>
          <version>1.4.1</version>
        </dependency>
        <dependency>
          <groupId>another.group.id</groupId>
          <artifactId>anotherId</artifactId>
          <version>1.0.1</version>
        </dependency>
      </dependencies>
    

    【讨论】:

      猜你喜欢
      • 2011-01-10
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      相关资源
      最近更新 更多