Set directory in Maven Antrun Plugin
Asked Answered
G

2

7

I've downloaded jquery-ui to my webapp which has a build.xml for compressing and minifying. Now I would like to run this build.xml from within my pom.xml:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <id>compile</id>
        <phase>compile</phase>
        <configuration>
          <target>
            <ant antfile="src/main/webapp/resources/js/jquery-ui/build/build.xml" />
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

But now the behavior for this buildfile is wrong. It creates a dist folder on the wrong place. Here's thebuild.xml` from jquery-ui: https://github.com/jquery/jquery-ui/blob/master/build/build.xml

It creates the dist folder on the same place where pom.xml is (same place i run mvn clean package) and it should create it in src/main/webapp/resources/js/jquery-ui/build

Is it possible to run build.xml from a specified directory (working directory)? Can't access the documentation for the Ant task: http://ant.apache.org/manual/CoreTasks/ant.html

EDIT: pom.xml

<?xml version="1.0" encoding="utf-8"?>
<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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.myproject</groupId>
  <artifactId>myproject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>myproject</name>

  <dependencies>
    <!-- ... -->
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <!-- ... -->
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <warName>myproject</warName>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>install</id>
            <phase>install</phase>
            <goals>
              <goal>sources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>compile</phase>
            <configuration>
              <executable>make</executable>
              <workingDirectory>src/main/webapp/resources/js/jquery</workingDirectory>
            </configuration>
            <goals>
              <goal>exec</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>compile</phase>
            <configuration>
              <target>
                <ant antfile="build.xml" dir="src/main/webapp/resources/js/jquery-ui/build" />
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

On running mvn clean package I'm getting the following errors in minify target of jquery-ui's build.xml:

[apply] build/minify-js.sh: Line 3: /home/danny/myproject/dist/jquery-ui-1.9pre/ui/minified/jquery-ui.min.js: File or directory not found

The correct path should be ${basedir}/src/main/webapp/resources/js/jquery-ui/build/dist/jquery-ui-1.9pre/ui/minified/jquery-ui.min.js instead of the above mentioned...

Glasgow answered 19/3, 2012 at 12:56 Comment(1)
I suggest you open a separate question for the exec-maven-plugin.Glacier
G
7

According to http://ant.apache.org/manual/Tasks/ant.html, you can use the dir attribute:

<mkdir dir="${project.build.directory}/jquery-ui" />
<ant antfile="src/main/webapp/resources/js/jquery-ui/build/build.xml" dir="${project.build.directory}/jquery-ui" />

On the other hand, are you really sure you want to create the build directory underneath your src directory? Shouldn't this go into target rather?

Glacier answered 19/3, 2012 at 13:37 Comment(7)
Oh, right... How the ant task should look like then? See edit above for my pom.xmlGlasgow
Did you try to add the dir attribute as shown in my example?Glacier
You can substitute target with ${project.build.directory}. I would append something that tells you the purpose of the directory, e.g. jquery-ui.Glacier
An Ant BuildException has occured: Basedir /home/danny/myproject/target/jquery-ui does not exist I need to have a resources folder beside WEB-INF to have the correct folder structure to access the resources.Glasgow
The error is caused by the target/jquery-ui directory not being there. I have changed the above example to create the directory first. Ant's mkdir task can be used for that.Glacier
I guess I need to specify another lifecycle phase for those plugins, because there are no files in target/myproject-1.0-SNAPSHOT. When files are copied in there I could run the build machanism for jquery+jquery-ui to only build it there... I'm a little bit confused now...Glasgow
You could also build it there, yes, absolutely.Glacier
T
0

I would recommend to use the maven plugin instead of Ant calls. Based on the docs maven-minify-plugin to do what you need. The plugin can be found in Maven Central. And furthermore you will find the maven-plugin-documention how to use the maven-plugin.

Tomfool answered 19/3, 2012 at 17:52 Comment(1)
But it would be more dynamically to use the provided build.xml from jquery ui. if something changes in folder structure/files will be added and so on i always have to update my code as well...Glasgow

© 2022 - 2024 — McMap. All rights reserved.