Friday, September 14, 2012

Use Ant task in Maven

Related posts:
Simple Java Application 


This is the simple Java Application that uses Ant task in Maven.

Environment:
Jdk1.7.07
Maven 3.0.4
Spring Source Tools Suits(STS) 3.0

Download source code app0105
To open the source code:
File --> Import --> Maven --> Existing Maven Projects

If you are new to Maven, please visit  Maven Tutorial
 





package org.sample.service;

public interface BeanService {
  public void sayGreeting();
}


package org.sample.service.impl;

import org.sample.service.BeanService;

public class BeanServiceImpl implements BeanService {
  private String greeting;

  public BeanServiceImpl() {}

  public BeanServiceImpl(String greeting) {
    this.greeting = greeting;
  }

  public void sayGreeting() {
    System.out.println(greeting);
  }

  public void setGreeting(String greeting) {
    this.greeting = greeting;
  }
}

package org.sample;

import org.sample.service.BeanService;
import org.sample.service.impl.BeanServiceImpl;


public class HelloApp {
    public static void main(String[] args) throws Exception {

        BeanService beanService = new BeanServiceImpl("Good morning");

        beanService.sayGreeting();
    }
}

<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>org.sample</groupId>
  <artifactId>app0105</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>Use Ant task in Maven</name>
  <url>http://maven.apache.org</url>

  <build>
    <finalName>app0105</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
     
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <dependencies>
          <dependency>
            <groupId>ant</groupId>
            <artifactId>ant-antlr</artifactId>
            <version>1.6.5</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <phase>install</phase>
            <configuration>
              <tasks>
                <echo>====================================================================</echo>
                <echo>=============================== RUNNING ============================</echo>
                <echo>====================================================================</echo>
                <java classname="org.sample.HelloApp">
                  <classpath>
                    <path refid="maven.runtime.classpath" />
                  </classpath>
                </java>
                <echo>====================================================================</echo>
                <echo>=============================== FINISHED ===========================</echo>
                <echo>====================================================================</echo>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>     
    </plugins>
  </build>
</project>

Result:
.
.
.
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (default) @ app0105 ---
[INFO] Executing tasks
     [echo] ====================================================================
     [echo] =============================== RUNNING ============================
     [echo] ====================================================================
Good morning
     [echo] ====================================================================
     [echo] =============================== FINISHED ===========================
     [echo] ====================================================================
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.296s
[INFO] Finished at: Sat Sep 15 14:40:17 JST 2012
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------


No comments:

Post a Comment