开发者

Integrating ant resource generation targets into a Maven build

开发者 https://www.devze.com 2023-03-09 22:34 出处:网络
I\'m currently working on a fairly large project that has been migrated from Ant to Maven. There are no problems with the actual build process (it compiles and packages the source code fine).

I'm currently working on a fairly large project that has been migrated from Ant to Maven. There are no problems with the actual build process (it compiles and packages the source code fine).

The problem is that I also have a lot of targets that generate additional resources for the project (compile LessCSS, generate & upload documentation, generate tld files for custom tags and functions etc.). I am not sure how I should handle these tasks. Let's take the target that builds CSS&JS as an example (the others are more or less similar, but not connected). It looks like this (simplified):

<target name="build.c开发者_开发问答ss_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>

In the pom.xml I have the following plugin set up:

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

The dependencies I'm using are no longer in our SVN repository (since they are managed by Maven), so I switched the libraries variable to point to the Maven repo:

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

This is not good, as that path may be valid only on my machine. I don't know any other way to reference the libraries from the Maven repository and I need them to run the ant targets.

  • Is my approach ok or is there a better way of doing things?
  • How do I get over the library problem?
  • Some resources are needed when packaging the project but some are not. Is there a lifecycle phase that is out of scope for compile/package? I found the site lifecycle which I think fits my needs.
  • Ideally, I should give up on the ant build file altogether, but I'm not sure it's worth the effort of making the scripts run as maven plugins (I currently have no idea how to do that). What do you think about this?

I'm new to Maven so any suggestions are appreciated.


Generally embedding antrun calls is not ideal, but if you've not found a suitable plugin to do what you need then I wouldn't worry about it. If the processing is fairly simple it is actually quite easy to embed it in a Maven plugin yourself, see this example for help getting started.

If you are going with antrun, and the dependency jars have already been installed to your Maven repository, you can configure the antrun plugin to use those jars in its execution by adding them as dependencies of the plugin configuration. This means the dependencies will be resolved and available for use, but not be visible to your project (useful to help avoid accidental inclusion). To then access them in a portable way you can use:

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

Alternatively you can use some of the other properties available to expose the Maven classpaths to the antrun plugin, for example ${maven.compile.classpath} See the antrun documentation for more details.

If you have multiple discrete executions for ant, you can configure them individually in the antrun plugin and specify a suitable id for each one. The example below shows two executions, both bound to the process-resources phase. Of course you need to supply some actual goals.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消