Sunday, 18 November 2012

Maven Build Tool Hacks

Apache Maven is a software project management and comprehension tool that finally builds an artifact, an artifact is a file, usually a JAR, that gets deployed to a Maven repository.

[1] generate archetype for a java project(maven-archetype-quickstart)
mvn archetype:generate \
-DgroupId=com.zazzercode \ 
-DartifactId=RobotMVC \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false



[2] convert project to support eclipse
$ mvn eclipse:eclipse
[3]  mvn dependency plugin
prayag@prayag:~/eccount$ mvn dependency:analyze
[4]  mvn versions plugin
prayag@prayag:~/eccount$ mvn versions:display-dependency-updates 
# displays all dependencies that have newer versions available.
The output looks like 
[...] 
[INFO] The following dependencies in Dependencies are using the newest version: 
[INFO]   com.github.gwtbootstrap:gwt-bootstrap ............... 2.1.0.0-SNAPSHOT 
[INFO]  com.google.inject:guice.......................................... 3.0 
[INFO]  com.google.inject.extensions:guice-assistedinject ................ 3.0 
[INFO]   com.octo.captcha:jcaptcha ................................ 2.0-alpha-1 
[INFO]   commons-dbcp:commons-dbcp ............................ 20030825.184428 
[INFO]   commons-fileupload:commons-fileupload .......................... 1.2.2


[5] run main class

mvn exec:java -Dexec.mainClass="com.pseudo.Shaharma"



[6] build 

[6.1] build a jar with dependencies

[STEP 1] Add the following <build> block to pom.xml

<project>
  <dependencies>
       [...]
  </dependencies>

  <build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.zazzercode.robot.App</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

</project>


[STEP 2] fire mvn command to build *.jar


<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <!-- NOTE: We don't need a groupId specification because the group is
             org.apache.maven.plugins ...which is assumed by default.
         -->
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        [...]
</project>

$ mvn clean compile assembly:single


[STEP 3] run *.jar at target/*.jar


$ java -jar target/RobotMVC-0.0.1-SNAPSHOT-jar-with-dependencies.jar


[5] install external jars so that can be added as maven dependencies

given a file in the same folder as I'm in, 

mvn install:install-file \
   -Dfile=creditVerifSystem.jar \
   -DgroupId=com.credit.verification \
   -DartifactId=credit-verification \
   -Dversion=1.0 \
   -Dpackaging=jar \
   -DgeneratePom=true

Now, it will be in ~/.m2

$ ll ~/.m2/repository/com/credit/verification/credit-verification/1.0/
total 48
-rw-r--r--  1 prayagupd  staff   193 Oct 22 21:11 _remote.repositories
-rw-r--r--  1 prayagupd  staff  4755 Jan 23  2010 credit-verification-1.0.jar
-rw-r--r--  1 prayagupd  staff   235 Oct 22 21:08 credit-verification-1.0.jar.lastUpdated
-rw-r--r--  1 prayagupd  staff   484 Oct 22 21:11 credit-verification-1.0.pom
-rw-r--r--  1 prayagupd  staff   235 Oct 22 21:08 credit-verification-1.0.pom.lastUpdated

Now, add it as maven dependency, 


<dependency>
          <groupId>com.credit.verification</groupId>
          <artifactId>credit-verification</artifactId>
          <version>1.0</version>
      </dependency>




References

No comments:

Post a Comment