加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

帮助设置Java构建环境

发布时间:2020-12-15 08:40:02 所属栏目:Java 来源:网络整理
导读:我的智慧结束了.我花了更多的时间来使我的构建工作,而不是实际开发软件.我目前正在开发基于Tomcat 6的大型 Java Web应用程序. 目前代码库大约是25k LOC(虽然这不是很有代表性,因为其中一些是为web服务自动生成的 – 但是它的重要性在于它很大)而且我一直专门
我的智慧结束了.我花了更多的时间来使我的构建工作,而不是实际开发软件.我目前正在开发基于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文件中调试我的拼写错误.

提前致谢

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读