【问题标题】:Help setting up a Java build environment帮助设置 Java 构建环境
【发布时间】:2011-05-04 12:13:22
【问题描述】:

我已经束手无策了。我花更多的时间让我的构建工作而不是实际开发软件。我目前正在开发基于 Tomcat 6 的大型 Java Web 应用程序。

目前的代码库大约是 25k LOC(虽然这不是很具有代表性,因为其中一些是为 Web 服务自动生成的——但要注意的是它很大)而且我一直在专门使用 eclipse 来做所有事情从调试,到构建路径管理到构建。在过去,eclipse 总是绰绰有余,但我从来没有做过这么大的项目。

我遇到的第一个问题是我开始添加 GWT 内容时。它工作正常,但构建过程有点棘手:我必须手动编译,然后将 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 文件中调试我的拼写错误。

提前致谢

【问题讨论】:

  • "... 25k LOC ... 但关键是它很大" - 对于“大”的某些定义:-)
  • @Stephen C,非常正确,我的意思是它不是一个简单的程序;-)

标签: java eclipse ant build maven


【解决方案1】:

我同意。你不需要 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>

【讨论】:

  • 我过去做过类似的事情,因为我在多个 Eclipse 项目中都需要它,所以我在一个地方定义了它,然后使用 antcall 从一个小的 Ant 脚本中触发它在每个项目中。
  • 这似乎比基本的 maven pom 更好,对于“普通”java 项目来说几乎是空的?
  • 你也可以在这里找到一些东西来帮助你开始sourceforge.net/apps/mediawiki/import-ant/…
  • 这很漂亮,谢谢@duffymo。它不像我想要的那样完全适合 eclipse,但我无法赢得所有...
【解决方案2】:

虽然 maven 乍一看可能有点吓人,但我认为它确实是一个非常不错的工具。是的,它确实有缺点,但总而言之,我认为它是 Java 的最佳选择之一。你真正需要了解的关于 Maven 的“事情”是一切都基于约定。如果你用传统的 maven 方式做所有事情,那么你的 maven pom 通常非常小,而且一切都“正常工作”。并且有很多入门信息可以向您展示“maven 约定”是什么。 (是的,你通常可以按照自己的方式做事,但通常不值得这样做,除非你被一个难以重新组织的现有项目所困)。

【讨论】:

  • 如果你不喜欢这个约定,你就会陷入困境。这是我非常讨厌 Maven 的原因之一。
  • @duffymo - 你没有被卡住。 maven 中的大多数东西都是可配置的(至少在核心插件中)。有时您会得到一个不尊重此配置的插件,这很痛苦,但对于具有大型插件生态系统的工具来说,这通常是正确的。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 或者您可以使用 duffymo 建议的更复杂的 ;-)
    • 感谢您的建议!这实际上是我的首选方法。我面临的最大问题是 eclipse 想要使用 Mac OS JDK 库,然后当我转向 Windows 时,它不知道到底发生了什么。我真的需要一些东西,可以根据操作系统确定 JDK 在编译时的位置。
    猜你喜欢
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2017-02-08
    相关资源
    最近更新 更多