Re: AW: AW: Javac Run By Ant Script is Unable to Find External Jars

Dennis Putnam <[email protected]>
Newsgroups gmane.comp.jakarta.ant.user
Message-ID <[email protected]>
Hi Jan,

I need to take a timeout. In my machinations I've created a new problem
that seems unrelated to ant. I'm trying to resolve that via the Eclipse
forum. I don't know if this new problem is masking the original one or
if it is related. I'll have to get back to you when I know which.

On 2/25/2019 9:15 AM, Jan Matèrne (jhm) wrote:
> A little sample:
> * src\java\org\apache\ant\Application.java
>   Sample application which uses Log4J2 as external dependency
> * src\resources\log4j2.xml
>   Configuration file for Log4J2
> * build.xml
>   Ant buildscript
>
> Run the script with "ant".
> Then start the application with "java -jar lib\Application.jar".
> The app will prompt all given arguments via Log4J2-Logger.
>
> Have a look at the "build" target and the resulting manifest inside the jar.
>
>
> Jan
>
>
> Application.java
> ----8-<------8-<------8-<------8-<------8-<--
> package org.apache.ant;
>
> import java.util.stream.Stream;
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.Logger;
>
>
> public class Application {
>
>     public static void main(String[] args) {
>         Logger logger = LogManager.getLogger(Application.class);
>         if (args.length == 0) {
>             logger.info("Application started without any argument.");
>         } else {
>             logger.info("Application started with %d arguments:%n", args.length);
>             Stream.of(args)
>                   .map( s -> "- '"+s+"'" )
>                   .forEach(logger::info);
>         }
>     }
>
> }
> ----8-<------8-<------8-<------8-<------8-<--
>
>
> log4j2.xml
> ----8-<------8-<------8-<------8-<------8-<--
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration status="WARN">
>     <Appenders>
>         <Console name="Console" target="SYSTEM_OUT">
>             <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
>         </Console>
>     </Appenders>
>     <Loggers>
>         <Root level="debug">
>             <AppenderRef ref="Console"/>
>         </Root>
>     </Loggers>
> </Configuration>
> ----8-<------8-<------8-<------8-<------8-<--
>
>
> build.xml
> ----8-<------8-<------8-<------8-<------8-<--
> <project name="Application" default="build">
>
>     <property name="build.dir" value="build"/>
>     <property name="src.dir" value="src"/>
>     <property name="lib.dir" value="lib"/>
>     <property name="main.class" value="org.apache.ant.Application"/>
>     
>     <property name="classes.dir" value="${build.dir}/classes"/>
>     <property name="java.src" value="${src.dir}/java"/>
>     <property name="resources.src" value="${src.dir}/resources"/>
>
>
>     <target name="clean">
>         <delete dir="${build.dir}"/>
>     </target>
>
>     <target name="download-dependencies">
>         <mkdir dir="${lib.dir}"/>
>         <!-- Performance improvement: don't download multiple times -->
>         <!-- TODO: available+get as macro -->
>         <available property="log4j-core.present" file="${lib.dir}/log4j-core-2.11.2.jar"/>
>         <get xmlns:unless="ant:unless"
>             unless:set="log4j-core.present"
>             src="https://search.maven.org/remotecontent?filepath=org/apache/logging/log4j/log4j-core/2.11.2/log4j-core-2.11.2.jar"
>             dest="${lib.dir}/log4j-core-2.11.2.jar" />
>         <available property="log4j-api.present" file="${lib.dir}/log4j-api-2.11.2.jar"/>
>         <get xmlns:unless="ant:unless"
>             unless:set="log4j-api.present"
>             src="https://search.maven.org/remotecontent?filepath=org/apache/logging/log4j/log4j-api/2.11.2/log4j-api-2.11.2.jar"
>             dest="${lib.dir}/log4j-api-2.11.2.jar" />
>     </target>
>
>     <target name="compile" depends="download-dependencies">
>         <mkdir dir="${classes.dir}"/>
>         <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false">
>             <classpath id="cp.runtime">
>                 <fileset dir="${lib.dir}" includes="**/*.jar"/>
>             </classpath>
>         </javac>
>     </target>
>
>     <target name="build" depends="compile">
>         <manifestclasspath property="jar.classpath"
>                            jarfile="${build.dir}/${ant.project.name}.jar">
>             <classpath refid="cp.runtime"/>
>         </manifestclasspath>
>         <jar destfile="${build.dir}/${ant.project.name}.jar">
>             <fileset dir="${classes.dir}"/>
>             <fileset dir="${resources.src}"/>
>             <manifest>
>                 <attribute name="Main-Class" value="${main.class}"/>
>                 <attribute name="Class-Path" value="${jar.classpath}"/>
>             </manifest>
>         </jar>
>     </target>
>
> </project>
> ----8-<------8-<------8-<------8-<------8-<--
>
>
>> -----Ursprüngliche Nachricht-----
>> Von: Jan Matèrne (jhm) [mailto:[email protected]]
>> Gesendet: Montag, 25. Februar 2019 14:08
>> An: 'Ant Users List'
>> Betreff: AW: AW: Javac Run By Ant Script is Unable to Find External
>> Jars
>>
>> Could you post the content of the manifest file and your directory
>> structure?
>>
>> I suppose that the paths are not correct …
>>
>>
>>
>> Jan
>>
>>
>>
>> Von: Dennis Putnam [mailto:[email protected]]
>> Gesendet: Montag, 25. Februar 2019 13:09
>> An: [email protected]
>> Betreff: Re: AW: Javac Run By Ant Script is Unable to Find External
>> Jars
>>
>>
>>
>> My apologies again but I have not made any progress on this problem. Is
>> there perhaps a better forum I should be using at this point? I did
>> make one discovery. It appears that although the application works in
>> Eclipse when I try to export it to a runnable jar using the Eclipse
>> wizard, the resulting jar fails as well. It seems to me that the
>> problem is not ant specific but something in the app build per se.
>>
>> On 2/21/2019 10:18 AM, Dennis Putnam wrote:
>>
>> I found an article on adding Maven dependencies to my ant script. While
>> I'm getting a successful build but the same exception when I run the
>> jar, I hope this is getting me closer. At least the pom.xml is in the
>> build. I am wondering if the added fileset is right and working. Here
>> is my latest:
>>
>> <?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="" />
>>         <property name="jarpath" value="/${user.home}/.m2/repository"
>> />
>>         <path id="maven-ant-tasks.classpath" path="/lib/jvm-
>> exports/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" />
>>         <artifact:pom id="pomfile" file="pom.xml" />
>>         <artifact:dependencies filesetId="mvn-dependencies"
>> pomRefId="pomfile" />
>>         <path id="compile-jars">
>>                 <multirootfileset basedirs="${jarpath}/commons-
>> io/2.5,${jarpath}/httpcomponents-client,${jarpath}/commons-
>> logging/1.2,${jarpath}/commons-codec/1.10,/lib/java-ext/json-simple-
>> 1.1.1">
>>                         <include name="commons-io-2.5.jar" />
>>                         <include name="httpclient-4.5.6.jar" />
>>                         <include name="httpcore-4.4.10.jar" />
>>                         <include name="commons-logging-1.2.jar" />
>>                         <include name="commons-codec-1.10.jar" />
>>                         <include name="json-simple-1.1.1.jar" />
>>                 </multirootfileset>
>>                 <fileset refid="mvn-dependencies" />
>>         </path>
>>         <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" classpathref="compile-jars" />
>>                 <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>
>>                 </jar>
>>         </target>
>> </project>
>>
>>
>> On 2/20/2019 3:22 PM, Jan Matèrne (jhm) wrote:
>>
>> If you have created your JAR the first step is done.
>>
>> Starting the JAR could be done in several ways. Common is that you have
>> to have all external classes on the runtime classpath:
>>
>>
>>
>> 1. Hard coded start script.
>>
>> Write a bash/bat-Script with the java command with all cp settings,
>> e.g. (bat)
>>
>> @echo off
>>
>> java -cp /build/myjar.jar;lib/one.jar;lib/two.jar;lib/three.jar
>> org.acme.Main %*
>>
>>
>>
>> 2. Wrapper script which collects all JARs in a dynamic way (see
>> ant.bat|ant.sh)
>>
>>
>>
>> 3. Use Ant + <java><classpath> for starting
>>
>>
>>
>> 4. Create a runnable JAR which references the external JARs (manifest:
>> main-class + classpath)
>>
>>
>>
>> 5. Create a shaded jar (uber jar, fat jar): include all classes from
>> external jars into your jar
>>
>>
>>
>> 6. Use a launcher which uses a dependency manager for getting the
>> classpath
>>
>>
>>
>>
>>
>>
>>
>> You don't have to replicate path definitions in your buildfile, you
>> could (and should) use references:
>>
>> <javac><classpath id="runtime.cp"><fileset dir="lib"
>> includes="**/*.jar"/>…
>>
>> <java><classpath refid="runtime.cp"/>
>>
>>
>>
>>
>>
>> You mave have a look at http://ant.apache.org/manual/tutorial-
>> HelloWorldWithAnt.html
>>
>>
>>
>>
>>
>> Jan
>>
>>
>>
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
signature.asc (application/pgp-signature, 196 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)

iEYEARECAAYFAlx0F0MACgkQ/fiWYqZ2tVSm2QCg5zYksnx/5DB9OBtGJKWh3r6A
ZboAn22QQnZA9jR+HdX5Nkmw7SkWPBNi
=DLfu
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.