帮助设置Java构建环境

时间:2011-05-04 00:11:30

标签: java eclipse ant build maven

我的智慧结束了。我花了更多的时间来使我的构建工作,而不是实际开发软件。我目前正在开发基于Tomcat 6的大型Java Web应用程序。

目前代码库大约是25k LOC(虽然这不是很有代表性,因为其中一些是为web服务自动生成的 - 但是它带走的是它很大)而且我一直专注于使用eclipse做任何事情从调试,到构建路径管理到构建。在过去,eclipse总是绰绰有余,但我以前从未参与过这么大的项目。

我遇到的第一个问题是我开始添加GWT内容时。它工作正常,但构建过程有点hacky:我不得不手动编译,然后将js输出复制到适当的目录,调试是一场噩梦(我发现其中一些问题只是GWT及其工作方式.. )。现在,我遇到的问题是尝试在Windows机器上处理项目(我曾经在Mac上工作,并且将不时继续使用Mac)。 Eclipse将Mac OS JVM库添加到构建路径中,当然,Windows无法找到。

asked a question about working in two different environments,大部分答案都与使用像Ant + Ivy或Maven这样的构建工具有关。我已经开始调查Maven并试图让我的项目使用它,但它只是令人头疼的一个接一个,我甚至没有开始尝试通过eclipse在Tomcat中实际执行/调试我的应用程序。所有这一切中最令人沮丧的部分是,对我来说,似乎这些构建工具非常复杂(并且公平,非常灵活)但是,至少在最初,当你试图弄清楚如何使生活变得更加困难时只需编译一个Java文件。

所有这些都说,有没有人建议如何简化这种情况?依赖管理会很好,但我不介意自己寻找JAR。我需要做的最重要的事情就是让我不知所措,让我继续研究我实际上正在尝试的工作,而不是花费数小时在构建系统使用的xml文件中调试我的拼写错误。

提前致谢

4 个答案:

答案 0 :(得分:4)

我同意。你不需要Maven;你不需要常春藤。从Ant开始。

这是一个相对通用的Ant build.xml。根据需要自定义。

<?xml version="1.0" encoding="UTF-8"?>
<project name="xslt-converter" basedir="." default="package">

    <property name="version" value="1.6"/>
    <property name="haltonfailure" value="no"/>

    <property name="out" value="out"/>

    <property name="production.src" value="src"/>
    <property name="production.lib" value="lib"/>
    <property name="production.resources" value="config"/>
    <property name="production.classes" value="${out}/production/${ant.project.name}"/>

    <property name="test.src" value="test"/>
    <property name="test.lib" value="lib"/>
    <property name="test.resources" value="config"/>
    <property name="test.classes" value="${out}/test/${ant.project.name}"/>

    <property name="exploded" value="out/exploded/${ant.project.name}"/>
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>

    <property name="reports.out" value="${out}/reports"/>
    <property name="junit.out" value="${reports.out}/junit"/>
    <property name="testng.out" value="${reports.out}/testng"/>

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">                            
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="testng.class.path">
        <fileset dir="${test.lib}">
            <include name="**/testng*.jar"/>
        </fileset>
    </path>

    <available file="${out}" property="outputExists"/>

    <target name="clean" description="remove all generated artifacts" if="outputExists">
        <delete dir="${out}" includeEmptyDirs="true"/>
        <delete dir="${reports.out}" includeEmptyDirs="true"/>
    </target>

    <target name="create" description="create the output directories" unless="outputExists">
        <mkdir dir="${production.classes}"/>
        <mkdir dir="${test.classes}"/>
        <mkdir dir="${reports.out}"/>
        <mkdir dir="${junit.out}"/>
        <mkdir dir="${testng.out}"/>
        <mkdir dir="${exploded.classes}"/>
        <mkdir dir="${exploded.lib}"/>
    </target>

    <target name="compile" description="compile all .java source files" depends="create">
        <!-- Debug output
                <property name="production.class.path" refid="production.class.path"/>
                <echo message="${production.class.path}"/>
        -->
        <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="production.class.path"/>
            <include name="**/*.java"/>
            <exclude name="**/*Test.java"/>
        </javac>
        <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="test.class.path"/>
            <include name="**/*Test.java"/>
        </javac>
    </target>

    <target name="junit-test" description="run all junit tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <junit printsummary="yes" haltonfailure="${haltonfailure}">
            <classpath refid="test.class.path"/>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${junit.out}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit.out}">
            <fileset dir="${junit.out}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${junit.out}" format="frames"/>
        </junitreport>
    </target>

    <taskdef resource="testngtasks" classpathref="testng.class.path"/>
    <target name="testng-test" description="run all testng tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2" parallel="methods" threadcount="50">
            <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="exploded" description="create exploded deployment" depends="testng-test">
        <copy todir="${exploded.classes}">
            <fileset dir="${production.classes}"/>
        </copy>
        <copy todir="${exploded.lib}">
            <fileset dir="${production.lib}"/>
        </copy>
    </target>

    <target name="package" description="create package file" depends="exploded">
        <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
    </target>

</project>

答案 1 :(得分:2)

虽然maven乍一看可能有点令人生畏,但我认为它确实是一个非常好的工具。是的,它确实有它的疣,但总而言之,我认为它是java的最佳选择之一。你真正需要了解maven的“事物”是一切都基于惯例。如果你按照传统的maven方式做所有事情,那么你的maven pom通常非常小,一切都“正常”。并且有很多入门信息可以向您展示“maven惯例”是什么。 (是的,你通常可以以自己的方式做事情,但除非你被困在一个很难重新组织的现有项目中,否则它通常是不值得的。)

答案 2 :(得分:1)

Ant / Ivy或Maven XML的替代品可能值得一试。他们使用真正的编程语言而不是XML DSL。

答案 3 :(得分:0)

仅在Eclipse中工作时可以使用的一个简化是让Eclipse负责编译。给定项目可以有自己的简单Ant脚本,它将bin中需要的任何内容打包到jar中。