问题
I am trying to use the default-template in my Java project to generate the launch script, based on the ivy dependencies specified in my project. The issue I am facing is that the ivy dependencies are getting picked up and put in the classpath but the main project jar is not getting included in the classpath.(Which gives a class not found exception while trying to run the script) On further investigation and comparison with other scripts I found that there is a line as.
export RUNTIME_CLASSPATH=""
in my generated script. This should have the path to my projects jar but is not getting populated (ex: export RUNTIME_CLASSPATH="$(dirname $0)/../lib/myproject.jar:"
)
Where exactly do we specify the project referring itself(calling project) in the script?? I have looked into all other files including the build properties, ivy, ivy settings to no avail. It would be great If someone could help me find what I am missing.
回答1:
ANT doesn't have any standard functionality for generating OS specific launch scripts. What you can do is generate an executable jar which can be launched by running java as follows:
java -jar demo.jar
The key feature of an executable jar is it's manifest that specifies both the main class and classpath:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_20-b20 (Sun Microsystems Inc.)
Main-Class: org.demo.App
Class-Path: lib/slf4j-api-1.6.4.jar lib/slf4j-simple-1.6.4.jar
The following ANT project demonstrates how ivy can assist in the creation of executable jars
Example
|-- build.xml
|-- ivy.xml
`-- src
|-- main
| `-- java
| `-- org
| `-- demo
| `-- App.java
`-- test
`-- java
`-- org
`-- demo
`-- AppTest.java
Run the build
ant clean build
Execute the program
java -jar build/dist/demo.jar
jar is packaged to use dependencies located in a relative "lib" subdirectory
build/dist/demo.jar
build/dist/lib/slf4j-api-1.6.4.jar
build/dist/lib/slf4j-simple-1.6.4.jar
ivy.xml
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="compile" description="Required to compile application"/>
<conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
<conf name="test" description="Required for test only" extends="runtime"/>
</configurations>
<dependencies>
<!-- compile dependencies -->
<dependency org="org.slf4j" name="slf4j-api" rev="1.6.4" conf="compile->default"/>
<!-- runtime dependencies -->
<dependency org="org.slf4j" name="slf4j-simple" rev="1.6.4" conf="runtime->default"/>
<!-- test dependencies -->
<dependency org="junit" name="junit" rev="4.10" conf="test->default"/>
</dependencies>
</ivy-module>
build.xml
<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">
<!--
================
Build properties
================
-->
<property name="src.dir" location="src/main/java"/>
<property name="test.src.dir" location="src/test/java"/>
<property name="build.dir" location="build"/>
<property name="classes.dir" location="${build.dir}/classes"/>
<property name="test.classes.dir" location="${build.dir}/test-classes"/>
<property name="ivy.reports.dir" location="${build.dir}/ivy-reports"/>
<property name="test.reports.dir" location="${build.dir}/test-reports"/>
<property name="dist.dir" location="${build.dir}/dist"/>
<property name="jar.main.class" value="org.demo.App"/>
<property name="jar.file" value="${dist.dir}/${ant.project.name}.jar"/>
<!--
===========
Build setup
===========
-->
<target name="init">
<ivy:resolve/>
<ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>
<ivy:cachepath pathid="compile.path" conf="compile"/>
<ivy:cachepath pathid="runtime.path" conf="runtime"/>
<ivy:cachepath pathid="test.path" conf="test"/>
</target>
<!--
===============
Compile targets
===============
-->
<target name="compile" depends="init">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
</target>
<target name="compile-tests" depends="compile">
<mkdir dir="${test.classes.dir}"/>
<javac srcdir="${test.src.dir}" destdir="${test.classes.dir}" includeantruntime="false" debug="true">
<classpath>
<path refid="test.path"/>
<pathelement path="${classes.dir}"/>
</classpath>
</javac>
</target>
<!--
============
Test targets
============
-->
<target name="test" depends="compile-tests">
<mkdir dir="${test.reports.dir}"/>
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="test.path"/>
<pathelement path="${classes.dir}"/>
<pathelement path="${test.classes.dir}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes" todir="${test.reports.dir}">
<fileset dir="${test.src.dir}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
</target>
<!--
=====================
Build and run targets
=====================
-->
<target name="build" depends="test">
<ivy:retrieve pattern="${dist.dir}/lib/[artifact]-[revision](-[classifier]).[ext]" conf="runtime"/>
<manifestclasspath property="jar.classpath" jarfile="${jar.file}">
<classpath>
<fileset dir="${dist.dir}/lib" includes="*.jar"/>
</classpath>
</manifestclasspath>
<jar destfile="${jar.file}" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${jar.main.class}" />
<attribute name="Class-Path" value="${jar.classpath}" />
</manifest>
</jar>
</target>
<target name="run" depends="build">
<java jar="${jar.file}" fork="true"/>
</target>
<!--
=============
Clean targets
=============
-->
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="clean-all" depends="clean">
<ivy:cleancache/>
</target>
</project>
来源:https://stackoverflow.com/questions/11457392/error-in-classpath-generation-from-default-template-ant