1

我是 Java 和 Travis CI 的新手,在配置我的项目时遇到了错误。我正在使用 ant 来构建,并使用 ivy 来管理依赖项。如果在本地找不到,我的 ant build 会直接下载 ivy jarfile。这在我的机器上运行良好,但当我推送到 Travis 时失败。

令人费解的是:它看起来像是在任务期间下载了jar并将其放入/home/travis/.ant/lib/bootstrap-ivy但在随后的任务中却找不到它resolve。知道发生了什么吗?完整的错误、我的 ant 构建文件和我.travis.yml的都包含在下面。

这是完整的 Travis 错误:

Using worker: worker-linux-3-1.bb.travis-ci.org:travis-linux-13
git.1
$ git clone --depth=50 --branch=master git://github.com/ecmendenhall/Java-TTT.git ecmendenhall/Java-TTT
Cloning into 'ecmendenhall/Java-TTT'...
remote: Counting objects: 613, done.
remote: Compressing objects: 100% (345/345), done.
remote: Total 613 (delta 325), reused 421 (delta 133)
Receiving objects: 100% (613/613), 102.29 KiB, done.
Resolving deltas: 100% (325/325), done.
$ cd ecmendenhall/Java-TTT
git.2
$ git checkout -qf 3a5201cc7e850d7cbad69712fce28a36c86ea6d1
$ jdk_switcher use oraclejdk7
Switching to Oracle JDK7 (java-7-oracle), JAVA_HOME will be set to /usr/lib/jvm/java-7-oracle
update-alternatives: error: no alternatives for apt.
update-alternatives: error: no alternatives for mozilla-javaplugin.so.
update-alternatives: error: no alternatives for apt.
$ java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
$ javac -version
javac 1.7.0_17
$ ant resolve
Buildfile: /home/travis/build/ecmendenhall/Java-TTT/build.xml
    [mkdir] Created dir: /home/travis/build/ecmendenhall/Java-TTT/lib
check-ivy:
     [echo] Checking for Ivy .jar in local directories.
bootstrap-ivy:
     [echo] Bootstrapping Ivy installation.
    [mkdir] Created dir: /home/travis/.ant/lib
      [get] Getting: http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar
      [get] To: /home/travis/.ant/lib/ivy.jar
resolve:
     [echo] Resolving project dependencies.
BUILD FAILED
/home/travis/build/ecmendenhall/Java-TTT/build.xml:52: Problem: failed to create task or type antlib:org.apache.ivy.ant:retrieve
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet
This appears to be an antlib declaration. 
Action: Check that the implementing library exists in one of:
        -/usr/share/ant/lib
        -/home/travis/.ant/lib
        -a directory added on the command line with the -lib argument
Total time: 3 seconds
The command "ant resolve" failed and exited with 1 during install.
Your build has been stopped.

这是我的蚂蚁构建文件:

<?xml version="1.0" encoding="UTF-8"?>
<project name="TicTacToe" basedir="." default="jar" xmlns:ivy="antlib:org.apache.ivy.ant">
    <description>Builds the TicTacToe command line application and its associated tests.</description>

    <!-- Source and build directory defaults -->
    <property name="src.dir" value="src"/>
    <property name="resources.dir" value="resources"/>

    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir" value="${build.dir}/jar"/>

    <property name="main-class" value="com.cmendenhall.Main"/>

    <mkdir dir="lib" />
    <property name="lib.dir" value="lib"/>
    <property name="test.dir" value="test" />

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>

    <path id="classpath.test">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
        <pathelement location="${classes.dir}" />
        <pathelement location="${resources.dir}" />
    </path>

    <path id="application" location="${jar.dir}/${ant.project.name}.jar"/>

    <!-- Automatically download Ivy -->
    <property name="ivy.jar.dir" value="${user.home}/.ant/lib" />
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />

    <target name="check-ivy" unless="INTERN-ivy.jar.exists" description="Check if the Ivy .jar is already installed.">
        <echo message="Checking for Ivy .jar in local directories." />
        <available property="INTERN-ivy.jar.exists" file="${ivy.jar.file}"/>
    </target>

    <target name="bootstrap-ivy"
            description="Installs the Ivy jar before downloading dependencies."
            unless="INTERN-ivy.jar.exists"
            depends="check-ivy">
        <echo message="Bootstrapping Ivy installation." />
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ivy.jar"
             src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
        <mkdir dir="/usr/share/ant/lib" />
        <get dest="/usr/share/ant/lib/ivy.jar"
             src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar" />
    </target>

    <target name="resolve" description="Retrieve dependencies with Ivy." depends="check-ivy, bootstrap-ivy">
        <echo message="Resolving project dependencies." />
        <ivy:retrieve />
    </target>

    <target name="clean" description="Cleans up automatically generated files and directories.">
        <echo message="Cleaning up build directory." />
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile" depends="resolve" description="Compiles project.">
        <echo message="Compiling project." />
        <mkdir dir="${classes.dir}"/>
        <javac includeantruntime="false" debug="true" srcdir="${src.dir}" destdir="${classes.dir}" >
            <classpath refid="classpath" />
        </javac>
    </target>

    <target name="compile-tests" depends="resolve" description="Compiles JUnit tests." >
        <echo message="Compiling JUnit tests." />
        <javac includeantruntime="false" debug="true" srcdir="${test.dir}" destdir="${classes.dir}" >
            <classpath refid="classpath" />
        </javac>
    </target>

    <target name="jar" depends="compile" description="Packages project as .jar file.">
        <echo message="Packaging project as .jar file." />
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar">
            <manifest>
                <attribute name="Main-class" value="${main-class}"/>
            </manifest>
            <fileset dir="${classes.dir}">
                <include name="**/*.class" />
            </fileset>
            <fileset dir="${resources.dir}">
                <include name="**/*.properties" />
            </fileset>
        </jar>
    </target>

    <property name="junitpath" refid="classpath.test" />


    <target name="test" depends="compile, compile-tests" description="Runs JUnit tests.">
        <echo message="Running JUnit tests." />
        <junit printsummary="on" haltonfailure="yes" failureproperty="test.failure">
            <jvmarg value="-Dfile.encoding=UTF-8:-XX:-UseSplitVerifier" />
            <formatter type="brief" usefile="false" />
            <classpath>
                <path refid="classpath.test"/>
            </classpath>

            <batchtest fork="on">
                <fileset id="matchedtests" dir="${classes.dir}">
                    <include name="**/*Test.class"/>
                    <exclude name="**/TicTacToeTest.class" />
                </fileset>
            </batchtest>
        </junit>
    </target>

    <target name="clean-build" depends="clean, jar" description="Cleans output directory and builds .jar.">
        <echo message="Cleaning build directory and rebuilding .jar." />
    </target>

    <target name="build-all" depends="clean, jar, test" description="Cleans output directory, retrieves dependencies, builds and packages project and tests.">
        <echo message="Finishing build." />
    </target>

</project>

这是我的.travis.yml

language: java
install: ant resolve
jdk:
  - oraclejdk7
  - openjdk7
  - openjdk6

更新:感谢以下建议,这是对我有用的解决方案。

我用官方文档中推荐的和任务替换了我的自定义bootstrap-ivy任务:download-ivyinit-ivy

    <property name="ivy.install.version" value="2.1.0-rc2" />
    <condition property="ivy.home" value="${env.IVY_HOME}">
      <isset property="env.IVY_HOME" />
    </condition>
    <property name="ivy.home" value="${user.home}/.ant" />
    <property name="ivy.jar.dir" value="${ivy.home}/lib" />
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />

    <target name="download-ivy" unless="offline">

        <mkdir dir="${ivy.jar.dir}"/>
        <!-- download Ivy from web site so that it can be used even without any special installation -->
        <get src="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar" 
             dest="${ivy.jar.file}" usetimestamp="true"/>
    </target>

    <target name="init-ivy" depends="download-ivy">
      <!-- try to load ivy here from ivy home, in case the user has not already dropped
              it into ant's lib dir (note that the latter copy will always take precedence).
              We will not fail as long as local lib dir exists (it may be empty) and
              ivy is in at least one of ant's lib dir or the local lib dir. -->
        <path id="ivy.lib.path">
            <fileset dir="${ivy.jar.dir}" includes="*.jar"/>

        </path>
        <taskdef resource="org/apache/ivy/ant/antlib.xml"
                 uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
    </target>

我添加了一个before_install步骤.travis.yml,它ant init-ivy在尝试解决依赖关系之前运行(以下是Travis 构建周期的文档):

language: java
before_install: ant init-ivy
install: ant resolve
jdk:
  - oraclejdk7
  - openjdk7
  - openjdk6

现在一切正常!

4

1 回答 1

2

这与 Ant 从.ant/lib. 这个文件夹在 Ant 启动时被查找,如果找到任何 jar,则将其添加到其核心类加载器中。这里在.ant/lib执行构建时正在填充,所以为时已晚。

解决此问题的两种选择:

于 2013-05-21T17:35:31.313 回答