Including Maven Dependencies in Build

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Including Maven Dependencies in Build

Dennis Putnam
I have a working ant build script but I changed my application to use
some maven dependencies. I am trying to include those dependencies into
that build script. I found this article for doing that:

https://stackoverflow.com/questions/7335819/ant-eclipse-complains-about-artifactdependencies

I know this article is old but I cannot find anything more recent that
is the equivalent. If anyone has anything I would certainly be
interested. Anyway as result I came up with this build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="KCBSEvents" default="jar" basedir="."
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
     <property name="build.properties" value="build.properties"/>
     <property name="resources" value="resource" />
     <property name="jardir" value="KCBSEvents" />
     <property name="KCBSDir" value="src/KCBSEvents" />
     <property name="member.number" value="000000" />
     <property name="member.name" value="" />
     <path id="maven-ant-tasks.classpath"
path="lib/maven-ant-tasks-2.1.3.jar" />
     <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
            uri="antlib:org.apache.maven.artifact.ant"
            classpathref="maven-ant-tasks.classpath" />
     <target name="mvn-init">
         <echo message="Loading maven dependencies" />
         <artifact:dependencies filesetid="maven-dependencies">
             <dependency groupId="org.apache.httpcomponents"
artifactId="httpclient" version="4.5.6" />
             <dependency groupId="commons-io" artifactId="commons-io"
version="2.5" />
         </artifact:dependencies>
     </target>
     <target name="checkOS">
         <condition property="isWindows">
             <os family="windows" />
         </condition>
         <condition property="isLinux">
             <os family="unix" />
         </condition>
     </target>
     <target name="if_windows" depends="checkOS" if="isWindows">
         <property name="jarfile" value="C:\temp\KCBSEvents.jar" />
         <property name="antcontrib" value="H:\html\Applets\ant-contrib" />
     </target>
     <target name="if_linux" depends="checkOS" if="isLinux">
         <property name="jarfile"
value="/tmp/${member.number}/KCBSEvents.jar" />
         <property name="antcontrib"
value="/var/www/html/Applets/ant-contrib/ant-contrib-1.0b3.jar" />
     </target>
     <target name="setclass" depends="if_linux,if_windows">
         <taskdef resource="net/sf/antcontrib/antcontrib.properties">
             <classpath>
                 <pathelement location="${antcontrib}" />
             </classpath>
         </taskdef>
     </target>
     <target name="incserial" depends="setclass">
         <copy todir="bin/${jardir}/${resources}">
             <fileset dir="${KCBSDir}/${resources}">
                 <include name="${build.properties}" />
             </fileset>
             <filterchain>
                 <expandproperties />
             </filterchain>
         </copy>
         <if> <isset Property="build.number" /> <then>
             <echo message="update build requested" />
         </then> <else>
             <echo message="new build requested" />
             <buildnumber />
         </else> </if>
         <propertyfile
file="bin/${jardir}/${resources}/${build.properties}">
             <entry key="serialnumber" value="${build.number}" />
             <entry key="membernumber" value="${member.number}" />
             <entry key="name" value="${member.name}" />
         </propertyfile>
         <echo message="serial number: ${build.number}" />
     </target>
     <target name="jar" description="Compile serialized jar"
depends="incserial,if_windows,if_linux,mvn-init">
         <echo message="Using destination file ${jarfile}" />
         <javac srcdir="src" destdir="bin"
classpathref="compile.classpath" includeantruntime="false" />
         <jar destfile="${jarfile}" basedir="bin">
             <manifest>
                 <attribute name="Manifest-Version" value="1.0"/>
                 <attribute name="Created-By" value="1.6.0 (Sun
Microsystems Inc.)" />
                 <attribute name="Main-Class"
value="KCBSEvents.KCBSEvents" />
             </manifest>
         </jar>
     </target>
</project>

When I run ant I get a successful build. However, when I try to run the
resulting jar file I get a traceback. The trackback seems to imply that
the maven dependencies were not pulled into the jar. Am I missing
something in the build that ties the dependencies to the jar? Can
someone help me debug this? TIA.

FWIW here is the resulting output from the build:

Buildfile: /usr/build/makejar.xml

checkOS:

if_linux:

if_windows:

setclass:

incserial:
      [echo] update build requested
[propertyfile] Updating property file:
/usr/build/bin/KCBSEvents/resource/build.
properties
      [echo] serial number: 1001

mvn-init:
      [echo] Loading maven dependencies

jar:
      [echo] Using destination file /tmp/60286/KCBSEvents.jar
       [jar] Building jar: /tmp/60286/KCBSEvents.jar

BUILD SUCCESSFUL
Total time: 3 seconds


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Including Maven Dependencies in Build

Jaikiran Pai-2
Hello Dennis,


On 07/12/18 1:15 AM, Dennis Putnam wrote:
>
>         <artifact:dependencies filesetid="maven-dependencies">
>
I don't see this fileset being used in the rest of the target(s) which
generate your jar file. Maybe you missed using it to copy over the
dependency files? Take a look at section "Using FileSets and the Version
Mapper" of http://maven.apache.org/ant-tasks/examples/dependencies.html
for an example on how you could use it.

-Jaikiran


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Including Maven Dependencies in Build

Dennis Putnam
On 12/7/2018 12:10 AM, Jaikiran Pai wrote:

> Hello Dennis,
>
>
> On 07/12/18 1:15 AM, Dennis Putnam wrote:
>>         <artifact:dependencies filesetid="maven-dependencies">
>>
> I don't see this fileset being used in the rest of the target(s) which
> generate your jar file. Maybe you missed using it to copy over the
> dependency files? Take a look at section "Using FileSets and the Version
> Mapper" of http://maven.apache.org/ant-tasks/examples/dependencies.html
> for an example on how you could use it.
>
> -Jaikiran
Hi Jaikiran,

Thanks for the reply. I thought it must have been some thing like that.
After reading your link and as a maven noob and not much better with
ant, I see I have to copy those dependencies. But it is not clear where
they need to be copied to. The example shows "lib" but I don't see how
javac knows to look there unless it is by default. My build directory is
a local git repository so the structure is "bin" and "src" in that
directory. There is no "lib" at the present time. That is also were I
have the build xml file for ant along with some files for obtaining some
information during the ant build. The output file for the jar is in a
directory in "/tmp" which is later copied to its final location.





signature.asc (203 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Including Maven Dependencies in Build

Dennis Putnam
On 12/7/2018 3:14 PM, Dennis Putnam wrote:

> On 12/7/2018 12:10 AM, Jaikiran Pai wrote:
>> Hello Dennis,
>>
>>
>> On 07/12/18 1:15 AM, Dennis Putnam wrote:
>>>         <artifact:dependencies filesetid="maven-dependencies">
>>>
>> I don't see this fileset being used in the rest of the target(s) which
>> generate your jar file. Maybe you missed using it to copy over the
>> dependency files? Take a look at section "Using FileSets and the Version
>> Mapper" of http://maven.apache.org/ant-tasks/examples/dependencies.html
>> for an example on how you could use it.
>>
>> -Jaikiran
> Hi Jaikiran,
>
> Thanks for the reply. I thought it must have been some thing like that.
> After reading your link and as a maven noob and not much better with
> ant, I see I have to copy those dependencies. But it is not clear where
> they need to be copied to. The example shows "lib" but I don't see how
> javac knows to look there unless it is by default. My build directory is
> a local git repository so the structure is "bin" and "src" in that
> directory. There is no "lib" at the present time. That is also were I
> have the build xml file for ant along with some files for obtaining some
> information during the ant build. The output file for the jar is in a
> directory in "/tmp" which is later copied to its final location.
>
I found another article that suggested adding a classpath. I did notice
that "compile.classpath" in my original xml was not set. I created a new
"build.classpath" and added a "lib" directory to my build directory.
Although I am getting the same results perhaps I am a bit closer, at
least it makes logical sense to me now. Here is my latest xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="KCBSEvents" default="jar" basedir="."
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
        <property name="build.properties" value="build.properties"/>
        <property name="resources" value="resource" />
        <property name="jardir" value="KCBSEvents" />
        <property name="KCBSDir" value="src/KCBSEvents" />
        <property name="member.number" value="000000" />
        <property name="member.name" value="" />
        <path id="maven-ant-tasks.classpath"
path="lib/maven-ant-tasks-2.1.3.jar" />
        <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
               uri="antlib:org.apache.maven.artifact.ant"
               classpathref="maven-ant-tasks.classpath" />
        <target name="mvn-init">
                <echo message="Loading maven dependencies" />
                <artifact:dependencies filesetid="maven-dependencies">
                        <dependency groupId="org.apache.httpcomponents"
artifactId="httpclient" version="4.5.6" />
                        <dependency groupId="commons-io"
artifactId="commons-io" version="2.5" />
                </artifact:dependencies>
                <copy todir="lib">
                        <fileset refid="maven-dependencies" />
                        <mapper type="flatten" />
                </copy>
        </target>
        <target name="checkOS">
                <condition property="isWindows">
                        <os family="windows" />
                </condition>
                <condition property="isLinux">
                        <os family="unix" />
                </condition>
        </target>
        <target name="if_windows" depends="checkOS" if="isWindows">
                <property name="jarfile" value="C:\temp\KCBSEvents.jar" />
                <property name="antcontrib"
value="H:\html\Applets\ant-contrib" />
        </target>
        <target name="if_linux" depends="checkOS" if="isLinux">
                <property name="jarfile"
value="/tmp/${member.number}/KCBSEvents.jar" />
                <property name="antcontrib"
value="/var/www/html/Applets/ant-contrib/ant-contrib-1.0b3.jar" />
        </target>
        <target name="setclass" depends="if_linux,if_windows">
                <taskdef resource="net/sf/antcontrib/antcontrib.properties">
                        <classpath>
                                <pathelement location="${antcontrib}" />
                        </classpath>
                </taskdef>
        </target>
        <target name="incserial" depends="setclass">
                <copy todir="bin/${jardir}/${resources}">
                        <fileset dir="${KCBSDir}/${resources}">
                                <include name="${build.properties}" />
                        </fileset>
                        <filterchain>
                                <expandproperties />
                        </filterchain>
                </copy>
                <if> <isset Property="build.number" /> <then>
                        <echo message="update build requested" />
                </then> <else>
                        <echo message="new build requested" />
                        <buildnumber />
                </else> </if>
                <propertyfile
file="bin/${jardir}/${resources}/${build.properties}">
                        <entry key="serialnumber" value="${build.number}" />
                        <entry key="membernumber"
value="${member.number}" />
                        <entry key="name" value="${member.name}" />
                </propertyfile>
                <echo message="serial number: ${build.number}" />
        </target>
        <path id="build.classpath">
                <pathelement location="lib" />
                <pathelement location="bin" />
        </path>
        <target name="jar" description="Compile serialized jar"
depends="incserial,if_windows,if_linux,mvn-init">
                <echo message="Using destination file ${jarfile}" />
                <javac srcdir="src" destdir="bin"
classpathref="build.classpath" includeantruntime="false" />
                <jar destfile="${jarfile}" basedir="bin">
                        <manifest>
                                <attribute name="Manifest-Version"
value="1.0"/>
                                <attribute name="Created-By"
value="1.6.0 (Sun Microsystems Inc.)" />
                                <attribute name="Main-Class"
value="KCBSEvents.KCBSEvents" />
                        </manifest>
                </jar>
        </target>
</project>

Somehow I am still not able to get ant to pull in the maven dependencies.

signature.asc (203 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Including Maven Dependencies in Build

Dennis Putnam
In reply to this post by Dennis Putnam
On 12/7/2018 12:10 AM, Jaikiran Pai wrote:
Hello Dennis,


On 07/12/18 1:15 AM, Dennis Putnam wrote:
        <artifact:dependencies filesetid="maven-dependencies">

I don't see this fileset being used in the rest of the target(s) which
generate your jar file. Maybe you missed using it to copy over the
dependency files? Take a look at section "Using FileSets and the Version
Mapper" of http://maven.apache.org/ant-tasks/examples/dependencies.html
for an example on how you could use it.

-Jaikiran
Hi Jaikiran,

Thanks for the reply. I thought it must have been some thing like that.
After reading your link and as a maven noob and not much better with
ant, I see I have to copy those dependencies. But it is not clear where
they need to be copied to. The example shows "lib" but I don't see how
javac knows to look there unless it is by default. My build directory is
a local git repository so the structure is "bin" and "src" in that
directory. There is no "lib" at the present time. That is also were I
have the build xml file for ant along with some files for obtaining some
information during the ant build. The output file for the jar is in a
directory in "/tmp" which is later copied to its final location.
Although no one was able to ultimately help me with this problem I did finally get it to work. It took a lot of digging and trying a lot of different suggested solutions before I found one that actually worked. To close out this thread for the archives, here is what I discovered:

It seems that 'ant' is not able to figure out how to handle the maven remote repositories or maven jar files, at least in any of the solutions I tried. I finally found that if I downloaded the maven jars manually and put them in the normal maven library (.m2/repository) I could then use the zipfileset directives to get them included. Although the library itself is really arbitrary. Here is my final build xml file, the bolded text is all I needed to change:

<?xml version="1.0" encoding="UTF-8"?>
<project name="KCBSEvents" default="jar" basedir=".">
    <property name="build.properties" value="build.properties"/>
    <property name="resources" value="resource" />
    <property name="jardir" value="KCBSEvents" />
    <property name="KCBSDir" value="src/KCBSEvents" />
    <property name="member.number" value="000000" />
    <property name="member.name" value="" />
    <target name="checkOS">
        <condition property="isWindows">
            <os family="windows" />
        </condition>
        <condition property="isLinux">
            <os family="unix" />
        </condition>
    </target>
    <target name="if_windows" depends="checkOS" if="isWindows">
        <property name="jarfile" value="C:\temp\KCBSEvents.jar" />
        <property name="antcontrib" value="H:\html\Applets\ant-contrib" />
    </target>
    <target name="if_linux" depends="checkOS" if="isLinux">
        <property name="jarfile" value="/tmp/${member.number}/KCBSEvents.jar" />
        <property name="antcontrib" value="/var/www/html/Applets/ant-contrib/ant-contrib-1.0b3.jar" />
    </target>
    <target name="setclass" depends="if_linux,if_windows">
        <taskdef resource="net/sf/antcontrib/antcontrib.properties">
            <classpath>
                <pathelement location="${antcontrib}" />
            </classpath>
        </taskdef>
    </target>
    <target name="incserial" depends="setclass">
        <copy todir="bin/${jardir}/${resources}">
            <fileset dir="${KCBSDir}/${resources}">
                <include name="${build.properties}" />
            </fileset>
            <filterchain>
                <expandproperties />
            </filterchain>
        </copy>
        <if> <isset Property="build.number" /> <then>
            <echo message="update build requested" />
        </then> <else>
            <echo message="new build requested" />
            <buildnumber />
        </else> </if>
        <propertyfile file="bin/${jardir}/${resources}/${build.properties}">
            <entry key="serialnumber" value="${build.number}" />
            <entry key="membernumber" value="${member.number}" />
            <entry key="name" value="${member.name}" />
        </propertyfile>
        <echo message="serial number: ${build.number}" />
    </target>
    <target name="jar" description="Compile serialized jar" depends="incserial,if_windows,if_linux">
        <echo message="Using destination file ${jarfile}" />
        <javac srcdir="src" destdir="bin" includeantruntime="false" />
        <jar destfile="${jarfile}" basedir="bin" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Manifest-Version" value="1.0"/>
                <attribute name="Created-By" value="ant 1.9.2 on CentOS 7" />
                <attribute name="Main-Class" value="KCBSEvents.KCBSEvents" />
            </manifest>
            <fileset dir="${user.home}/.m2/repository" />
            <zipfileset excludes="META-INF/*.SF" src="${user.home}/.m2/repository/commons-io/2.5/commons-io-2.5.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${user.home}/.m2/repository/httpcomponents-client/httpclient-4.5.6.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${user.home}/.m2/repository/httpcomponents-client/httpcore-4.4.10.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${user.home}/.m2/repository/commons-logging/1.2/commons-logging-1.2.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${user.home}/.m2/repository/commons-codec/1.10/commons-codec-1.10.jar"/>
        </jar> 
    </target>
</project>

signature.asc (203 bytes) Download Attachment