帮助设置Java构建环境

帮助设置Java构建环境,java,eclipse,ant,build,maven,Java,Eclipse,Ant,Build,Maven,我束手无策。我把更多的时间花在让构建工作上,而不是实际开发软件上。我目前正在开发基于Tomcat6的大规模Java web应用程序 目前,代码库大约是25k LOC(虽然这不太具有代表性,因为其中一些代码是为web服务自动生成的——但需要注意的是它很大),我一直在专门与eclipse合作,以完成从调试、构建路径管理到构建的所有工作。在过去,eclipse已经足够了,但我以前从未参与过这么大的项目 我遇到的第一个问题是当我开始添加GWT内容时。它工作正常,但构建过程有点麻烦:我必须手动编译js输出

我束手无策。我把更多的时间花在让构建工作上,而不是实际开发软件上。我目前正在开发基于Tomcat6的大规模Java web应用程序

目前,代码库大约是25k LOC(虽然这不太具有代表性,因为其中一些代码是为web服务自动生成的——但需要注意的是它很大),我一直在专门与eclipse合作,以完成从调试、构建路径管理到构建的所有工作。在过去,eclipse已经足够了,但我以前从未参与过这么大的项目

我遇到的第一个问题是当我开始添加GWT内容时。它工作正常,但构建过程有点麻烦:我必须手动编译js输出,然后将其复制到适当的目录中,调试是一场噩梦(我意识到其中一些问题只是GWT及其工作方式…)。现在,我遇到的问题是试图在Windows机器上运行该项目(我一直在Mac上工作,并将不时继续在Mac上工作)。Eclipse将Mac OS JVM库添加到构建路径中,当然Windows找不到

我和大多数答案都与使用Ant+Ivy或Maven之类的构建工具有关。我已经开始研究Maven并试图让我的项目使用它,但这只是一个接一个的头痛问题,我甚至还没有开始尝试通过eclipse在Tomcat中实际执行/调试我的应用程序。最令人沮丧的是,在我看来,这些构建工具异常复杂(公平地说,非常灵活),但至少在最初,当您试图找出如何简单地编译Java文件时,会让生活变得更加困难

这么说来,有人对如何简化这个场景有什么建议吗?依赖关系管理会很好,但我不介意自己寻找罐子。我所需要的最大的东西就是能让我不碍事地去做我真正想做的事情,而不是花几个小时去调试构建系统使用的xml文件中的打字错误


提前谢谢

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

这里是一个相对通用的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>


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

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

  • (基于Ruby)
  • (基于Groovy)

虽然maven乍一看可能有点令人生畏,但我认为它确实是一个非常好的工具。是的,它确实有缺点,但总而言之,我认为它是java的最佳选择之一。关于maven,你真正需要了解的是,一切都是基于惯例的。如果你用传统的maven方式做每件事,那么你的maven pom通常非常小,而且每件事都“正常工作”。还有大量的入门信息可以告诉你“maven约定”是什么。(是的,你通常可以用自己的方式做事情,但通常不值得,除非你被困在一个难以重新组织的现有项目中)。

或者你可以使用duffymo建议的更复杂的项目;-)谢谢你的建议!这实际上是我喜欢的方法。不过,我面临的最大问题是eclipse想要使用MacOSJDK库,当我转到windows时,它不知道到底发生了什么。我真的需要一些能够根据操作系统确定JDK在编译时的位置的东西。我在过去做过类似的事情,因为我在多个Eclipse项目中需要它,所以我在一个地方定义了它,然后使用
antcall
从每个项目中的一个小Ant脚本触发它。这似乎比基本的maven pom好,对于一个“普通”的java项目来说,它几乎是空的?你也可以在这里找到一些东西让你开始这很漂亮,谢谢@duffymo。它不像我所希望的那样完全适合eclipse,但我不能赢得所有这些………25k LOC…但需要注意的是它很大”-对于“大”的某些定义-@Stephen C,非常正确,我的观点是它不是一个简单的程序;-)如果你不喜欢这个大会,你就被卡住了。这是我最讨厌Maven的地方之一。@duffymo-你没有被卡住。maven中的大部分内容都是可配置的(至少在核心插件中是如此)。有时你会得到一个不尊重这个配置的插件,这是一件痛苦的事情,但是对于一个拥有大量插件的工具来说,这通常是正确的。