Thursday, July 18, 2013

Download a library from maven manually

There are times when you want to manually download a maven library using command line then use the local version in your pom file.

First you can download a library from maven using the following command. For example the following mvn command downloads a a library from maven and installs it in your local maven repo.


mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get -Dartifact=org.springframework:spring-instrument:3.1.1.RELEASE -DrepoUrl=repository.sonatype.org/content/repositories/central

or something like so

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get  -Dartifact=com.restfuse:com.eclipsesource.restfuse:1.2.0 -DrepoUrl=repository.sonatype.org/content/repositories/central


Then in your maven config you can add the following snippit in your pom. 

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
    <version>3.1.1</version>
   <!--Optionally if you downloaded it to some non-standard folder    
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/spring-instrument-3.1.1.jar</systemPath>
   -->
</dependency>









Wednesday, July 3, 2013

Wrapping an existing ant target so you can pass in a javaagent and other jvm args

If you have an ant task that requires a javaagent such as spring's instrument.jar,
you might get this stack error message.

Caused by: java.lang.IllegalStateException: ClassLoader [org.apache.tools.ant.loader.AntClassLoader5] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar



Solution is to wrap the offending ant target with the javaagent:org.springframework.instrument.jar




So:
if your build.xml   file has the following target,  

  <target name="create-sql">
       .... code that requires javaagent
    </target>

then you would add a new wrapper target, like so:

 <target name="build-create-sql" >
        <!--wrapping spring insturment jar-->
            <java  classname="org.apache.tools.ant.launch.Launcher"
                    fork="true"
                    failonerror="true"
                    dir="."
                    timeout="4000000"
                    taskname="startAnt">
                    <classpath>
                        <pathelement location="${ant.home}/lib/ant-launcher.jar"/>
                    </classpath>
                    <jvmarg value="-javaagent:${spring.instrument.path}"/>
                     <jvmarg value="-XX:MaxPermSize=512M" />
                    <jvmarg value="-Xmx1024M" />
                    <arg value="-buildfile"/>
                    <arg file="build.xml"/>
                    <arg value="create-sql"/>
              </java>

    </target>



Now you can pass in a spring instrument jar as a jvmarg and javaagent in your new ant target and the existing target does not have to be modified.