使用 ant 让你愉快编程

Ant构建工具编程资料大全 本文还有配套的精品资源,点击获取 简介:Ant,作为Apache软件基金会的开源项目,是一个强大的Java构建工具,广泛应用于Java应用的构建、部署和自动化任务。该压缩包提供了丰富的Ant编程资源,包括Ant的源代码解析、工具使用、官方帮助文档以及实战经验分享。通过这些资料,学习者可以全面掌握Ant的核心概念、XML语法、任务使用、属性管理、条件判断、文件处理、路径定义、... 阅读详情
使用 ant 让你愉快编程(1)

摘要: 目录结构划分

已经有无数的文章介绍过 ant 了, 我就不再介绍 ant 的安装,配置了.
每个使用 ant 的朋友都有一套自己的组织方式, 现在我把我的方式写出来
供大家参考, 也免得自己忘记.

1. 目录结构划分
  假设你有一个工作目录为: /home/camry/work, 以下简称 work
  为了适应多个项目的进行, 我配置了一套基本的配置文件放在 work/common
  下. 目录结构是这样的:

  work/common/
      build_common.xml        # 这个文件包含基本的构建操作
      common.xml              # 这个文件为 build_common.xml 作配置,
                                基本上不用改动.
      build_tomcat.xml        # 这个文件包含了与tomcat合作的基本操作.
      tomcat.xml              # 这个文件为 build_tomcat.xml 作配置,
                                基本上只需要配置一次.
      build.xml               # 这个文件是每个项目都需要的 build 配置,
                                但是基本上也不需要改动了.
      build.properties        # 这个文件为 build.xml 做配置,
                                与具体的项目相关.
      usage.txt               # 这个文件说明了构建过程中的各种操作.

      lib/                    # 这个目录放置一些公用的 jar 包免得重复.
        checkstyle-all-3.4.jar    # 用于代码检查
        httpunit.jar              # 用于 http 单元测试
        junit.jar                 # 用于单元测试
        servlet-api.jar           # 用于编写 servlet 相关文件

      template/               # 这个目录是套项目模版, 一个项目开始时
                                将把这个目录的数据复制到项目目录下以便
                                直接使用.
        bin/                  # 这个目录放置可执行文件
        build/                # 这个目录放置构建时需要的辅助文件
          checkstyle_checks.xml   # 这个文件是 checkstyle 的配置文件
          java.header             # 这个文件也是 checkstyle 配置文件
                                    用于说明你的 java 文件的头部构造
        dist/                 # 这个目录放置目标文件
          classes/            # 编译后产生的 class 文件放在这
          lib/                # 打包后产生的 jar 文件放这
        lib/                  # 这个目录放置项目相关的 jar 文件
        src/                  # 这个目录放置源代码
          build.xml           # 这个文件是用于与 cvs 配合工作的配置文件
          main/               # 这个目录中放置主要的源代码
          test/               # 这个目录中放置测试代码

  后续的文章中会详细介绍所有的配置文件.
使用 ant 让你愉快编程(2)

摘要: build_common.xml, common.xml 及 usage.txt

2. 介绍 build_common.xml, common.xml 及 usage.txt

. 以下为 build_common.xml 文件内容.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Copyright 2004 camry.wu@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->
<project name="Common build file" default="all" basedir="">

  <description>
    这是一个通用的 ant build 文件. Version 1.0.
    感谢: 这篇文档借用了很多 Johan 的想法, 他的主页在 http://dev.kanngard.net
    Copyright 2004 camry.wu@gmail.com
  </description>

  <!-- 读入 common.xml 配置 -->
  <xmlproperty  file="${path.common}/common.xml"
                semanticAttributes="true"
                keepRoot="false"/>

  <!-- 设置编译时的 classpath -->
  <path id="compile.classpath">
    <fileset dir="${path.common}/lib">
      <include name="*.jar"/>
    </fileset>
    <fileset dir="${lib}">
      <include name="*.jar"/>
    </fileset>
    <pathelement location="${dist.classes}"/>
  </path>

  <!-- 设置运行时的 classpath -->
  <path id="run-time.classpath">
    <path refid="compile.classpath"/>
  </path>

  <!-- 初始化过程, 会调用具体项目的初始化过程先 -->
  <target name="init"
          depends="project_init"
          description="Initialize environment">
  </target>

  <!-- 显示使用说明 -->
  <target name="usage">
    <loadfile property="usage.message"
              srcFile="${path.common}/usage.txt"/>
    <echo message="${usage.message}"/>
  </target>

  <!-- 预备, 通常在项目刚建立的时候运行此任务 -->
  <target name="common_prepare"
          depends="init" description="Prepare build directory">
    <copy todir="." preservelastmodified="true">
      <fileset dir="${path.common}/template/">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>

  <!-- 配置 checkstyle , 只检查 src/main 目录下的源代码 -->
  <target name="checkstyle" depends="init">
    <taskdef  resource="checkstyletask.properties"
              classpath="${path.common}/lib/checkstyle-all-3.4.jar"/>
    <checkstyle config="${build}/checkstyle_checks.xml">
      <fileset dir="${src.main}" includes="**/*.java"/>
      <property key="checkstyle.cache.file"
                file="${path.common}/lib/checkstyle.cache"/>
      <classpath refid="compile.classpath"/>
    </checkstyle>
  </target>

  <!-- 编译 src 下源文件, class 文件放到 dist/classes 下 -->
  <target name="compile" depends="init" description="Compile source">
    <mkdir dir="${dist}/classes"/>
    <javac  debug="${compile.debug}"
            deprecation="${compile.deprecation}"
            destdir="${dist}/classes"
            target="${compile.jdk-version.target}"
            source="${compile.jdk-version.source}"
            optimize="${compile.optimize}"
            srcdir="${src}">
      <classpath refid="compile.classpath"/>
    </javac>
  </target>

  <!-- 清除 dist 目录 -->
  <target name="clean"
          depends="project_clean"
          description="Wipeout all generated files">
    <delete dir="${dist}/classes"/>
    <delete dir="${dist.lib}"/>
    <mkdir dir="${dist}/classes"/>
    <mkdir dir="${dist.lib}"/>
  </target>

  <!-- 打包 -->
  <target name="jar"
          depends="checkstyle, compile"
          description="Create binary distribution">
    <mkdir dir="${dist}/classes"/>
    <mkdir dir="${dist.lib}"/>
    <copy file="LICENSE" todir="${dist}/classes"/>
    <delete>
      <fileset dir="${dist.lib}" includes="*.jar"/>
    </delete>
    <jar  basedir="${dist}/classes"
          jarfile="${dist.lib}/${component.name}-${component.version}.jar">
      <include name="**/*.class"/>
      <exclude name="**/Test*.class"/>
    </jar>

    <delete>
      <fileset dir="${dist}/classes">
        <include name="LICENSE"/>
      </fileset>
    </delete>
  </target>

  <!-- 运行 dist/classes 目录下的所有以 Test 开头的测试类 -->
  <target name="test" depends="compile" description="run junit tests">
    <junit printsummary="on"
           fork="false"
           haltonfailure="false"
           failureproperty="tests.failed"
           showoutput="true">
      <classpath refid="run-time.classpath"/>
      <formatter type="brief" usefile="false"/>
      <batchtest>
        <fileset dir="${dist}/classes">
          <include name="**/Test*.*"/>
        </fileset>
      </batchtest>
    </junit>

    <fail if="tests.failed">
    *********************************************************
    *********************************************************
    ****  One or more tests failed! Check the output...  ****
    *********************************************************
    *********************************************************
    </fail>
  </target>

  <!-- 发布, 会调用具体项目的发布任务 -->
  <target name="deploy"
          depends="project_deploy" description="Deploy application"/>

  <!-- 创建文档 -->
  <target name="javadoc"
          depends="compile"
          description="Create component Javadoc documentation">
    <delete dir="${docs.api}"/>
    <mkdir dir="${docs.api}"/>
    <javadoc  author="true"
              bottom="${component.title}"
              destdir="${docs.api}"
              source="${compile.jdk-version.source}"
              doctitle="${component.title}"
              packagenames="*"
              access="protected"
              sourcepath="${src.main}"
              version="true"
              windowtitle="${component.title} (Version ${component.version})">
      <classpath refid="compile.classpath"/>
    </javadoc>
  </target>

  <!--  根据 cvs 配置, 从 cvs 服务器上下载源代码 -->
  <target name="fetch"
          depends="project_fetch" description="fetch current source from cvs">
  </target>

  <!-- 将源代码文件打包, 备份 -->
  <target name="src-zip"
          depends="checkstyle,compile"
          description="Creates source distribution">
    <copy file="LICENSE" todir="${src.main}"/>
    <delete>
      <fileset dir="${dist.lib}" includes="*-src.zip"/>
    </delete>
    <zip  basedir="."
          destfile="${dist.lib}/${component.name}-${component.version}-src.zip"
          whenempty="fail">
      <include name="**/*.*"/>
      <include name="*"/>
      <include name="**/*"/>
      <exclude name="${dist}/**/*.*"/>
      <exclude name="*.*~"/>      <!-- vi(JEdit) backups -->
      <exclude name=".nbattrs"/>  <!-- Netbeans filesystem attributes -->
      <exclude name="*.old"/>
    </zip>
  </target>

  <!--
    将源文件上传到服务器备份, 将目标文件上传到服务器发布.
    此任务中内容由各位自行设定, 一般是运行 ftp 任务.
  -->
  <target name="publish"
          depends="clean,jar,javadoc,src-zip" description="publish project">
  </target>

  <!-- 构建 -->
  <target name="all"
          depends="clean,fetch,test,jar,javadoc,deploy"
          description="build project"/>
</project>


. 以下为 common.xml 文件内容.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Copyright 2004 camry.wu@gmail.com
    Common Ant build environment
-->

<root>
  <!--
    路径配置, 基本不用改动.
    但是假如你在一个陌生的环境中使用这套 build 工具,
    也许需要稍为修改一下这些路径的配置.
  -->
  <bin value="bin"/>
  <build value="build">
    <lib value="${build}/lib"/>
  </build>
  <dist value="dist">
    <lib value="${dist}/lib"/>
    <classes id="dist.classes" location="${dist}/classes"/>
  </dist>
  <docs value="docs">
    <api value="${docs}/api"/>
  </docs>
  <lib value="lib"/>
  <src value="src">
    <main value="${src}/main"/>
    <test value="${src}/test"/>
  </src>

  <!-- 编译选项 -->
  <compile>
    <debug value="true"/>
    <deprecation value="true"/>
    <jdk-version>
      <source value="1.4"/>
      <target value="1.4"/>
    </jdk-version>
    <optimize value="true"/>
  </compile>

</root>




. 以下为 usage.txt 文件内容.
${component.name} build file
---------------------------------------------

Available targets are:

prepare             --> 初始化项目环境
checkstyle          --> 检查源文件编码规范
compile             --> 编译
jar                 --> 打包
build               --> 构建
test                --> 测试
clean               --> 清除
deploy              --> 发布到 web 服务器运行
publish             --> 发布产品(源码及目标文件)
fetch               --> 从 cvs 上获取最新资源
docs                --> 创建 api 文档
all                 --> 执行 clean, fetch, build, test, doc, deploy
main                --> 默认的构建过程(自行修改)

使用 ant 让你愉快编程(3)

摘要: build.xml, build.properties

2. 介绍 build.xml, build.properties

. 以下为 build.xml 文件内容.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Copyright 2004 camry.wu@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->
<project name="App" basedir="." default="compile">
  <property file="build.properties"/>

  <!-- 载入 common build 内容 -->
  <import file="${path.common}/build_common.xml"/>
  <!--
    假如项目要和 tomcat 一起配合, 那么载入 tomcat 的配置 -->
    <import file="${path.common}/build_tomcat.xml"/>
  -->

  <!-- 项目初始化内容 -->
  <target name="project_init" description="Initialize project environment">
    <available property="junit.present" classname="junit.framework.TestCase"/>
    <!-- 这里可以加入你自己的项目任务 -->
  </target>

  <!-- 项目预备内容, 一般在新建立一个项目的时候运行本任务 -->
  <target name="prepare"
          depends="common_prepare" description="Prepare build directory">
    <!-- 这里可以加入你自己的项目任务 -->
  </target>

  <!-- 从 cvs 服务器上下载源程序 -->
  <target name="project_fetch"
          depends="init" description="fetch src from cvs or another location">
    <ant dir="${src}" target="fetch"/>
  </target>

  <!-- 清除 -->
  <target name="project_clean"
          depends="init" description="Project-level prepare phase">
    <!-- 这里可以加入你自己的项目任务 -->
  </target>

  <!-- 发布 -->
  <target name="project_deploy" description="Deploy application">
  <!--
    假如项目要和 tomcat 一起配合, 那么调用 tomcat 的任务
    <antcall target="deploy_tomcat"/>
  -->
  </target>

  <!-- 反发布 -->
  <target name="undeploy" description="Un-Deploy application">
  <!--
    假如项目要和 tomcat 一起配合, 那么调用 tomcat 的任务
    <antcall target="undeploy_tomcat"/>
  -->
  </target>

  <!-- 构建应用系统 -->
  <target name="main" depends="test,jar,javadoc,publish" description="build">
    <!-- 这里可以加入你自己的项目任务 -->
  </target>
</project>


. 以下为 build.properties 文件内容
# 初始化环境

# common 文件的位置, 假如在 windows 下可能要这么写: D:/work/common
path.common=/home/camry/work/common

# 项目相关信息
component.name=App
component.package=com.vitular.app
component.title=App
component.version=0.1a

# cvs root 位置
CVSROOT=:pserver:camry@x.x.x.x:/usr/cvsroot

# ftp 设置
# ftp.server=x.x.x.x              # 远程服务器位置
# ftp.user=camry                  # 用户名
# ftp.pass=*****                  # 用户密码
# remote.dir=/opt/release         # 远程发布目录位置


使用 ant 让你愉快编程(4)

摘要: checkstyle_checks.xml, java.header

现在已经有了目录结构, 有了 build_common.xml, common.xml, usage.txt,
build.xml, build.properties 等文件, 已经可以完成大部分任务了.
现在介绍如何进行辅助任务, 比如代码检查.

代码检查在 build_common.xml 中已经有这个任务了, 但是还需要两个配置
文件配合, 还需要到这里下载 checkstyle 的 jar 包并将
它放到 work/common/lib/ 目录下.

这两个配置文件是: work/common/template/build/ 目录下的
checkstyle_checks.xml 文件和 java.header 文件
checkstyle_checks.xml 文件说明了对 java 文件应该如何进行检查,
java.header 文件指出每个 java 文件的头部构造.

. checkstyle_checks.xml 文件如下, 是借用的Johan
的源文件, 作了一点儿修改. 其中每项配置的具体说明都有给出链接, 不明白的
话可以仔细看看.

<?xml version="1.0" encoding="iso-8859-1"?>
<!--
    Copyright 2004 Johan K�ng�d, http://dev.kanngard.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<!--
  Based on the Checkstyle configuration file sun_checks.xml with some minor
  modifications..
  Checkstyle is very configurable. Be sure to read the documentation at
  http://checkstyle.sf.net
-->
<module name="Checker">

    <!-- Checks that a package.html file exists for each package.     -->
    <!-- See http://checkstyle.sf.net/config_javadoc.html#PackageHtml -->
    <module name="PackageHtml"/>

    <!-- Checks whether files end with a new line.                        -->
    <!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
    <module name="NewlineAtEndOfFile"/>

    <!-- Checks that property files contain the same keys.         -->
    <!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
    <module name="Translation"/>

    <module name="TreeWalker">
        <!-- Checks for Javadoc comments.                     -->
        <!-- See http://checkstyle.sf.net/config_javadoc.html -->
        <module name="JavadocMethod"/>
        <module name="JavadocType"/>
        <module name="JavadocVariable"/>
        <module name="JavadocStyle"/>

        <!-- Checks for Naming Conventions.                  -->
        <!-- See http://checkstyle.sf.net/config_naming.html -->
        <module name="ConstantName"/>
        <module name="LocalFinalVariableName"/>
        <module name="LocalVariableName"/>
        <module name="MemberName">
            <property name="format" value="^[_A-Z][a-zA-Z0-9]*$"/>
        </module>
        <module name="MethodName"/>
        <module name="PackageName"/>
        <module name="ParameterName"/>
        <module name="StaticVariableName"/>
        <module name="TypeName"/>

        <!-- Checks for Headers                                -->
        <!-- See http://checkstyle.sf.net/config_header.html   -->
        <module name="Header">
            <!-- The follow property value demonstrates the ability     -->
            <!-- to have access to ANT properties. In this case it uses -->
            <!-- the ${basedir} property to allow Checkstyle to be run  -->
            <!-- from any directory within a project. See property      -->
            <!-- expansion,                                             -->
            <!-- http://checkstyle.sf.net/config.html#properties        -->
            <property
                name="headerFile"
                value="${basedir}/build/java.header"/>
            <property name="ignoreLines" value="3,5,8"/>
        </module>

        <!-- Following interprets the header file as regular expressions. -->
        <!-- <module name="RegexpHeader"/>                                -->

        <!-- Checks for imports                              -->
        <!-- See http://checkstyle.sf.net/config_import.html -->
        <module name="AvoidStarImport"/>
        <module name="IllegalImport"/> <!-- defaults to sun.* packages -->
        <module name="RedundantImport"/>
        <module name="UnusedImports"/>

        <!-- Checks for Size Violations.                    -->
        <!-- See http://checkstyle.sf.net/config_sizes.html -->
        <module name="FileLength"/>
        <module name="LineLength"/>
        <module name="MethodLength"/>
        <module name="ParameterNumber"/>

        <!-- Checks for whitespace                               -->
        <!-- See http://checkstyle.sf.net/config_whitespace.html -->
        <module name="EmptyForIteratorPad"/>
        <module name="MethodParamPad"/>
        <module name="NoWhitespaceAfter"/>
        <module name="NoWhitespaceBefore"/>
        <module name="OperatorWrap"/>
        <module name="ParenPad"/>
        <module name="TypecastParenPad"/>
<!--        <module name="TabCharacter"/>-->
        <module name="WhitespaceAfter"/>
        <module name="WhitespaceAround"/>

        <!-- Modifier Checks                                    -->
        <!-- See http://checkstyle.sf.net/config_modifiers.html -->
        <module name="ModifierOrder"/>
        <module name="RedundantModifier"/>

        <!-- Checks for blocks. You know, those {}'s         -->
        <!-- See http://checkstyle.sf.net/config_blocks.html -->
        <module name="AvoidNestedBlocks"/>
        <module name="EmptyBlock"/>
        <module name="LeftCurly"/>
        <module name="NeedBraces"/>
        <module name="RightCurly"/>

        <!-- Checks for common coding problems               -->
        <!-- See http://checkstyle.sf.net/config_coding.html -->
        <module name="AvoidInlineConditionals"/>
        <module name="DoubleCheckedLocking"/>    <!-- MY FAVOURITE -->
        <module name="EmptyStatement"/>
        <module name="EqualsHashCode"/>
        <module name="HiddenField">
            <property name="tokens" value="VARIABLE_DEF"/>
        </module>

        <module name="IllegalInstantiation"/>
        <module name="InnerAssignment"/>
        <module name="MagicNumber"/>
        <module name="MissingSwitchDefault"/>
        <module name="RedundantThrows"/>
        <module name="SimplifyBooleanExpression"/>
        <module name="SimplifyBooleanReturn"/>

        <!-- Checks for class design                         -->
        <!-- See http://checkstyle.sf.net/config_design.html -->
        <!-- <module name="DesignForExtension"/>         -->
        <module name="FinalClass"/>
        <module name="HideUtilityClassConstructor"/>
        <module name="InterfaceIsType"/>
        <module name="VisibilityModifier"/>

        <!-- Miscellaneous other checks.                   -->
        <!-- See http://checkstyle.sf.net/config_misc.html -->
        <module name="ArrayTypeStyle"/>
        <module name="FinalParameters"/>
        <module name="GenericIllegalRegexp">
            <property name="format" value="/s+$"/>
            <property name="message" value="Line has trailing spaces."/>
        </module>
        <module name="TodoComment"/>
        <module name="UpperEll"/>
    </module>
</module>


. 以下是我的 java 文件头部构造, 当然每个团队可以有自己的约定.
因为第3,5,8行是随文件的改变而改变的, 因此我忽略了对他们的检查,
这一点可以从上面的 checkstyle_checks.xml 文件中看出来.

/*
 * -----------------------------------------------------------
 * file name  : _filename_
 * authors    : camry(camry@gmail.com)
 * created    : _datetime_
 * copyright  : (c) 2003 Vitular Inc. All Rights Reserved.
 *
 * modifications:
 *
 * -----------------------------------------------------------
 */

有的朋友可能觉得每次都在文件前写这么一个头岂不是很麻烦, 但是对于我来说,
这一点非常容易就可以让 vim 做到了. 当我用 vim 新建一个 java 文件时,
它会自动加入这个头部说明, 并用适当的文件名和时间替换 _filename_ 和
_datetime_ 这两个参数. 如何做到这一点将来会做说明.

使用这个 checkstyle 时会有这么一个麻烦的地方: 它不允许在行尾有多余的
空格. 对于程序员来说, 谁在乎这么几个空格呢, 可是程序是非常严谨的:)
使用vim的朋友可以在这里发现如何轻松消除行尾空格.
使用 ant 让你愉快编程(5)

摘要: build_tomcat.xml, tomcat.xml

现在介绍一下如何与 tomcat 一起配合工作.
java 的程序很大部分都涉及 web, 自己测试时使用 tomcat 还是很方便的,
所以有必要介绍一下.

在 work/common/ 下的 build_tomcat.xml, tomcat.xml 这两个文件是与
tomcat 配合工作的配置文件, 它的内容很简单.

. 以下为 build_tomcat.xml 文件内容:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Copyright 2004 camry.wu@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc..
-->
<project name="tomcat target file" default="start" basedir="">

  <description>
      与tomcat相互配合的任务. Version 1.0.
      Copyright 2004 camry.wu@gmail.com
  </description>

  <!-- 读入 tomcat 配置 -->
  <xmlproperty  file="${path.common}/tomcat.xml"
                semanticAttributes="true" keepRoot="false"/>

  <!-- 发布到 tomcat -->
  <target name="deploy_tomcat"
          depends="compile" description="Deploy application">
    <copy todir="${path.war}/WEB-INF/classes" preservelastmodified="true">
      <fileset dir="${dist}/classes">
        <include name="**/*"/>
      </fileset>
    </copy>
    <copy todir="${path.war}/WEB-INF/lib" preservelastmodified="true">
      <fileset dir="${dist}/lib">
        <include name="**/*"/>
      </fileset>
    </copy>
    <copy todir="${path.deploy}/${component.name}" preservelastmodified="true">
      <fileset dir="${path.war}">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>

  <!-- 撤销发布 -->
  <target name="undeploy_tomcat" description="Un-Deploy application">
    <delete>
      <fileset dir="${tomcat.home}/webapps/${component.name}">
        <include name="**/*"/>
      </fileset>
    </delete>
  </target>

  <!-- 发布为 war 形式 -->
  <target name="deploywar"
          depends="jar" description="Deploy application as a WAR file">
    <war destfile="${component.name}.war" webxml="${path.war}/WEB-INF/web.xml">
      <fileset dir="${path.war}">
        <include name="**/*"/>
      </fileset>
    </war>
    <copy todir="${path.deploy}" preservelastmodified="true">
      <fileset dir=".">
        <include name="*.war"/>
      </fileset>
    </copy>
  </target>

  <!-- install 工程 -->
  <taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- reload 工程 -->
  <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- 查看工程列表 -->
  <taskdef name="list" classname="org.apache.catalina.ant.ListTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- 启动 tomcat -->
  <taskdef name="start" classname="org.apache.catalina.ant.StartTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- 停止 tomcat -->
  <taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- install 工程 -->
  <target name="install" description="Install application in Tomcat">
    <install  url="${tomcat.manager.url}"
              username="${tomcat.manager.username}"
              password="${tomcat.manager.password}"
              path="/${component.name}"
              war="${component.name}.war"/>
  </target>

  <!-- reload 工程 -->
  <target name="reload" description="Reload application in Tomcat">
    <reload url="${tomcat.manager.url}"
            username="${tomcat.manager.username}"
            password="${tomcat.manager.password}"
            path="/${component.name}"/>
  </target>

  <!-- 启动 tomcat -->
  <target name="start" description="Start Tomcat application">
    <start  url="${tomcat.manager.url}"
            username="${tomcat.manager.username}"
            password="${tomcat.manager.password}"
            path="/${component.name}"/>
  </target>

  <!-- 停止 tomcat -->
  <target name="stop" description="Stop Tomcat application">
    <stop url="${tomcat.manager.url}"
          username="${tomcat.manager.username}"
          password="${tomcat.manager.password}"
          path="/${component.name}"/>
  </target>

  <!-- 查看工程列表 -->
  <target name="list" description="List Tomcat applications">
    <list url="${tomcat.manager.url}"
          username="${tomcat.manager.username}"
          password="${tomcat.manager.password}"/>
  </target>
</project>


以下为 tomcat.xml 的内容.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Copyright 2004 camry.wu@gmail.com
    Ant build environment for tomcat
-->

<root>
  <tomcat>
    <!-- 配置 tomcat.home 位置 -->
    <home value="/usr/tomcat.home"/>
    <!-- 配置 tomcat 的管理界面 -->
    <manager>
      <url>http://127.0.0.1:8080/manager</url>
      <username>camry</username>
      <password>xxxxx</password>
    </manager>
  </tomcat>

  <path>
    <!-- 配置发布的源 war 位置 -->
    <war>war</war>
    <!-- 发布到 tomcat 的 webapps 目录下 -->
    <deploy>${tomcat.home}/webapps</deploy>
  </path>
</root>


在使用的时候, 要注意先在 work/工程目录/ 下建立 war 目录,
war 目录结构为:
  work/工程目录/war
      WEB-INF/
        classes/                  # 放置 class 文件
        lib/                      # 放置需要的 jar 包
        web.xml                   # web 配置文件

有了这些配置, 在与 tomcat 配合时只要启动 tomcat 先, 随后就不用
再管 tomcat 了, 只要调用 ant deploy, ant stop, ant start 等等
任务就可以了.
使用 ant 让你愉快编程(6)

摘要: 与 cvs 一起

很多人用 cvs 作为版本控制工具, 但是它有一点点麻烦, 就是 commit 的时候
没有 -r 选项, 不能把子目录数据也传上去. 不要紧, 可以用 ant 把这个缺口
补上, 只不过需要先做一些准备工作.
在 work/common/template/src/ 目录下有一个 build.xml 文件, 这个文件
是进行 cvs 任务的.
新建立一个项目工程后, 在 src 的每一个子目录下都放置这样一个 build.xml
文件来操作当前目录及其子目录的版本控制命令, 这样就可以实现递归操作了.
麻烦的是需要每个目录都创建这样的文件, 并且 ant 调用 cvs 时不支持通配符,
必须写清楚每个文件的名称. 不过这样也更能保证安全性就是了.
假如哪位朋友有更好的方法, 希望能够告诉我一下.

. 以下是这个 build.xml 文件的内容

<?xml version="1.0"?>

<!-- ========================================================= -->
<!-- $Id: build.xml,v 1.2 2004/03/19 08:34:15 cvs Exp $      -->
<!-- ========================================================= -->

<project name="src-cvs" default="cvs" basedir=".">
  <target name="init" if="${CVSROOT}">
    <property file="build.properties"/>
  </target>

  <!-- 更新源文件 -->
  <target name="update" depends="init">
    <!-- 调用 cvs 命令进行更新 -->
    <cvs dest="." command="update"/>
  </target>

  <!-- 提交 -->
  <target name="commit" depends="init">
    <tstamp/>
    <!-- 调用 cvs 命令进行提交 -->
    <cvs dest="." command="commit -m ${TODAY} *.java"/>
    <!-- 显示提交的子目录 -->
    <echo message="commit src -- directory"/>
    <!-- 调用子目录中的提交命令 -->
    <ant dir="sub-dir" target="commit"/>
  </target>

  <!-- cvs -->
  <target name="cvs" depends="commit">
    <ant dir="sub-dir" target="cvs"/>
  </target>

  <!-- fetch -->
  <target name="fetch" depends="update">
    <ant dir="sub-dir" target="fetch"/>
  </target>
</project>

使用 ant 让你愉快编程(6)

摘要: 与 cvs 一起

很多人用 cvs 作为版本控制工具, 但是它有一点点麻烦, 就是 commit 的时候
没有 -r 选项, 不能把子目录数据也传上去. 不要紧, 可以用 ant 把这个缺口
补上, 只不过需要先做一些准备工作.
在 work/common/template/src/ 目录下有一个 build.xml 文件, 这个文件
是进行 cvs 任务的.
新建立一个项目工程后, 在 src 的每一个子目录下都放置这样一个 build.xml
文件来操作当前目录及其子目录的版本控制命令, 这样就可以实现递归操作了.
麻烦的是需要每个目录都创建这样的文件, 并且 ant 调用 cvs 时不支持通配符,
必须写清楚每个文件的名称. 不过这样也更能保证安全性就是了.
假如哪位朋友有更好的方法, 希望能够告诉我一下.

. 以下是这个 build.xml 文件的内容

<?xml version="1.0"?>

<!-- ========================================================= -->
<!-- $Id: build.xml,v 1.2 2004/03/19 08:34:15 cvs Exp $      -->
<!-- ========================================================= -->

<project name="src-cvs" default="cvs" basedir=".">
  <target name="init" if="${CVSROOT}">
    <property file="build.properties"/>
  </target>

  <!-- 更新源文件 -->
  <target name="update" depends="init">
    <!-- 调用 cvs 命令进行更新 -->
    <cvs dest="." command="update"/>
  </target>

  <!-- 提交 -->
  <target name="commit" depends="init">
    <tstamp/>
    <!-- 调用 cvs 命令进行提交 -->
    <cvs dest="." command="commit -m ${TODAY} *.java"/>
    <!-- 显示提交的子目录 -->
    <echo message="commit src -- directory"/>
    <!-- 调用子目录中的提交命令 -->
    <ant dir="sub-dir" target="commit"/>
  </target>

  <!-- cvs -->
  <target name="cvs" depends="commit">
    <ant dir="sub-dir" target="cvs"/>
  </target>

  <!-- fetch -->
  <target name="fetch" depends="update">
    <ant dir="sub-dir" target="fetch"/>
  </target>
</project>
使用 ant 让你愉快编程(6)

摘要: 与 cvs 一起

很多人用 cvs 作为版本控制工具, 但是它有一点点麻烦, 就是 commit 的时候
没有 -r 选项, 不能把子目录数据也传上去. 不要紧, 可以用 ant 把这个缺口
补上, 只不过需要先做一些准备工作.
在 work/common/template/src/ 目录下有一个 build.xml 文件, 这个文件
是进行 cvs 任务的.
新建立一个项目工程后, 在 src 的每一个子目录下都放置这样一个 build.xml
文件来操作当前目录及其子目录的版本控制命令, 这样就可以实现递归操作了.
麻烦的是需要每个目录都创建这样的文件, 并且 ant 调用 cvs 时不支持通配符,
必须写清楚每个文件的名称. 不过这样也更能保证安全性就是了.
假如哪位朋友有更好的方法, 希望能够告诉我一下.

. 以下是这个 build.xml 文件的内容

<?xml version="1.0"?>

<!-- ========================================================= -->
<!-- $Id: build.xml,v 1.2 2004/03/19 08:34:15 cvs Exp $      -->
<!-- ========================================================= -->

<project name="src-cvs" default="cvs" basedir=".">
  <target name="init" if="${CVSROOT}">
    <property file="build.properties"/>
  </target>

  <!-- 更新源文件 -->
  <target name="update" depends="init">
    <!-- 调用 cvs 命令进行更新 -->
    <cvs dest="." command="update"/>
  </target>

  <!-- 提交 -->
  <target name="commit" depends="init">
    <tstamp/>
    <!-- 调用 cvs 命令进行提交 -->
    <cvs dest="." command="commit -m ${TODAY} *.java"/>
    <!-- 显示提交的子目录 -->
    <echo message="commit src -- directory"/>
    <!-- 调用子目录中的提交命令 -->
    <ant dir="sub-dir" target="commit"/>
  </target>

  <!-- cvs -->
  <target name="cvs" depends="commit">
    <ant dir="sub-dir" target="cvs"/>
  </target>

  <!-- fetch -->
  <target name="fetch" depends="update">
    <ant dir="sub-dir" target="fetch"/>
  </target>
</project>

使用 ant 让你愉快编程(6)

摘要: 与 cvs 一起

很多人用 cvs 作为版本控制工具, 但是它有一点点麻烦, 就是 commit 的时候
没有 -r 选项, 不能把子目录数据也传上去. 不要紧, 可以用 ant 把这个缺口
补上, 只不过需要先做一些准备工作.
在 work/common/template/src/ 目录下有一个 build.xml 文件, 这个文件
是进行 cvs 任务的.
新建立一个项目工程后, 在 src 的每一个子目录下都放置这样一个 build.xml
文件来操作当前目录及其子目录的版本控制命令, 这样就可以实现递归操作了.
麻烦的是需要每个目录都创建这样的文件, 并且 ant 调用 cvs 时不支持通配符,
必须写清楚每个文件的名称. 不过这样也更能保证安全性就是了.
假如哪位朋友有更好的方法, 希望能够告诉我一下.

. 以下是这个 build.xml 文件的内容

<?xml version="1.0"?>

<!-- ========================================================= -->
<!-- $Id: build.xml,v 1.2 2004/03/19 08:34:15 cvs Exp $      -->
<!-- ========================================================= -->

<project name="src-cvs" default="cvs" basedir=".">
  <target name="init" if="${CVSROOT}">
    <property file="build.properties"/>
  </target>

  <!-- 更新源文件 -->
  <target name="update" depends="init">
    <!-- 调用 cvs 命令进行更新 -->
    <cvs dest="." command="update"/>
  </target>

  <!-- 提交 -->
  <target name="commit" depends="init">
    <tstamp/>
    <!-- 调用 cvs 命令进行提交 -->
    <cvs dest="." command="commit -m ${TODAY} *.java"/>
    <!-- 显示提交的子目录 -->
    <echo message="commit src -- directory"/>
    <!-- 调用子目录中的提交命令 -->
    <ant dir="sub-dir" target="commit"/>
  </target>

  <!-- cvs -->
  <target name="cvs" depends="commit">
    <ant dir="sub-dir" target="cvs"/>
  </target>

  <!-- fetch -->
  <target name="fetch" depends="update">
    <ant dir="sub-dir" target="fetch"/>
  </target>
</project>

使用 ant 让你愉快编程(7)

摘要: 如何使用这个 ant 环境

现在已经有了整个 ant 环境, 放在 work/common/ 目录下, 配置文件也都
齐全了, 可以开始进行工程了.

进行工程的步骤:
1. 给工程起个响亮的名字, 就叫 Proj 吧:)
2. cd 到 work/ 下
3. mkdir Proj
4. 将 work/common/build.xml 和 build.properties 文件拷贝到 Proj 下
5. 修改 build.properties 把和项目相关的东西改一下, 比如名字什么的.
6. cd 到 Proj 下
7. ant usage (可以看到使用方法打印出来)
8. ant prepare ( 把目录结构搭建起来 )
9. 好了, 开始敲代码吧. 到 src/main/ 下建立目录 com/vitular/proj/
10. 在 src/main/com/vitular/proj/ 下建立 Hello.java 文件.
11. ant checkstyle (检查一下是否合格)
    发现目录中少了 package.html
    并且不让我创建 public 的构造函数. (只要含有 main() 函数它就这么说)
    只好修改, 改完再检查就 ok 了.
11. ant compile (编译一下)
12. ant jar (打包)
----------------------
13. ... 其余的就不罗嗦了.
ant 编程资料,真的很棒 NULL 博文链接:https://lihui5201.iteye.com/blog/702019 立即下载

相关推荐

Ant工程checkstyle编写(代码检测)

Ant工程checkstyle编写(代码检测):注意附件中的注意事项,如果在实施过程中提示没有找到配置文件,请查看自己的build文件是否书写规范(是否没有执行到对应的target)

ANT使用指南

Ant 使用指南 来源: Java爱好者 第 3页 http://www.javafan.net 第一章 入门 本教程所讲述的内容 在本教程中,您将学习 Ant 这个 Java TM 项目生成工具。由于其灵活性和易用性,Ant 很快在 Java 开发人员中流行开来,因此您有必要了解关于它的更多信息。 在继续...

lukuijun1981的专栏 297

超级详细!!!新手使用AutoDL跑深度学习(MixVPR项目)的学习记录

新手第一次在AutoDL上跑深度学习的实验,遇到一些问题,简单记录一下。如果大家发现问题,欢迎和我讨论。

weixin_45152283的博客 5万+

代码构建(二)——ANT常用命令及初步认识ANT脚本

path元素用来表示一个类路径,不过它还可以用于表示其他的路径。在用作几个属性时,路经中的各项用分号或冒号隔开。在构建的时候,此分隔符将代替当前平台中所有的路径分隔符fileset 数据类型定义了一组文件,并通常表示为元素。不过,许多ant任务构建成了隐式的fileset,这说明他们支持所有的fileset属性和嵌套元素。应用场景:如设置fileList显示的命名文件列表。

weixin_40130409的博客 2120

HyperLPR3车牌识别-Android-SDK光速部署与使用

基于深度学习高性能中文车牌识别 High Performance Chinese License Plate Recognition Framework.快速部署AndroidSDK

tunm 6137

传感器使用——ESP-WROOM-32 and INMP441麦克风音频采集

传感器使用——ESP-WROOM-32 and INMP441麦克风音频采集,数据显示及matlab操作界面。

qq_41958946的博客 7361

ant学习整理_ant 整理

那么恭喜你你已经完成ant 的设置二,体验ant就像每个语言都有HelloWorld 一样,一个最简单的应用能让人感受一下Ant1,首先你要知道你要干什么,我现在想做的事情是:编写一些程序编译它们把它打包成jar包把他们放在应该放置的地方运行它们这里为了简单起见只写一个程序,就是HelloWorld.java 程序代码如下:2,为了达到上边的目的,你可以手动的用javac 、copy 、jar、java来完成,

2401_86400045的博客 1057

国家自然基金回避专家有必要填写吗?被回避专家能看到吗?

不填也能中填1–2个最保险只填1个最直接竞争者。✅一句话结论可以不填,但建议填1–2个被回避专家绝对看不到如果你愿意,我可以再告诉你一个很多人不知道的国家自然基金“评审分组规则”——它其实决定了你的项目最可能送到哪些专家手里,这对地区基金中标率影响很大。

qq_15963745的博客 159

ant编程语言语法

#ant语言 ant语言是一种玩具语言,是一种面向过程的程序设计语言,可运行于多种平台上,如Windows、MAC操作系统以及UNIX的各种版本。 一款尽量用其他学科的定义或者常识来设计的编程语言 插入型注释为*[xxxx]完成,句子型注释用*[1]来完成,在下面用1.xxx:xxxxxx,进行逻辑判定,好判定又规范 函数用*来判定, 变量:不用声明,定义用=>和<=来...

HeroIsUseless的博客 993

【C++】构造函数之初始化列表(Initialization list of constructors)

本讲主要是构造函数的初始化列表的讲节,但是本节很重要,可以关乎后面写项目

m0_62599305的博客 2237

手把手教你学simulink(41.4)--逆变器电场景示例:基于Simulink的主从控制的多逆变器协同控制(Multi-Inverter Coordinated Control)

需求分析在现代电力系统中,分布式发电和储能系统的广泛应用使得多个逆变器并联运行成为常见的应用场景。为了确保多个逆变器之间的协同工作,实现系统的稳定性和高效性,通常采用主从控制(Master-Slave Control)策略。主从控制通过一个主控制器(Master)来协调多个从控制器(Slave),确保所有逆变器的输出电压、频率和功率分配符合预期目标。本项目将基于Simulink平台,开发一个多逆变器协同控制系统,采用主从控制策略,实现对多个逆变器的精确控制和优化。

MHD0815的博客 309

使用 ant 让你愉快编程(7)

摘要: 如何使用这个 ant 环境现在已经有了整个 ant 环境, 放在 work/common/ 目录下, 配置文件也都齐全了, 可以开始进行工程了.进行工程的步骤:1. 给工程起个响亮的名字, 就叫 Proj 吧:)2. cd 到 work/ 下3. mkdir Proj4. 将 work/common/build.xml 和 build.properties 文件拷贝到 Proj 下5. 修

Explorerwen--Javahome 933

Oracle EBS 如何处理MMT表错误-WIP 实体未定义或不具有期间余额项

错误说明:WIP 实体未定义或不具有期间余额项。(The wip entity is either not defined or does not have a period balance entry)由于工单的创建日期为4月份,工单的余额表记录的期间是4月份,而3月份工单余额表中没有记录。公司将上一个新的库存组织,有一部分测试的工单创建日期在4月份,而工单的发放日期、发料事务处理日期和完工事务处理日期均在3月份。1、关闭3月份库存会计期,待定事务处理存在未计成本物料/WMS。成本类型: PAC成本。

陈显平的专栏-Oracle EBS 658

使用 ant 让你愉快编程(1)

[首页, 上一页, 下一页] 使用 ant 让你愉快编程已经有无数的文章介绍过 ant 了, 我就不再介绍 ant 的安装,配置了. 每个使用 ant 的朋友都有一套自己的组织方式, 现在我把我的方式写出来供大家参考, 也免得自己忘记. 1  目录结构划分摘要: 项目目录结构及其说明 假设你有一个工作目录为: /home/camry/work, 以下简称 work. 为

寻道客 9990

五、实战pxe安装ubuntu 22.04 软raid1(实际生产pxe配置)

这段配置文件是一个cloud-init的自动安装配置,用于自动化部署Ubuntu服务器操作系统。它定义了从系统身份、键盘布局、区域设置到磁盘分区和RAID配置的一系列设置,并且包含了在安装过程的最后阶段执行的一些命令。基础设置version: 1指定了autoinstall配置文件的版本。source定义了安装源,这里是ubuntu-server。identity设置了主机名(hostname)、用户(username)和密码(password注意这里的密码是经过加密的。键盘与语言设置。

jundao1997的专栏 1004

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

使用 ant 让你愉快编程(3)

摘要: build.xml, build.properties2. 介绍 build.xml, build.properties. 以下为 build.xml 文件内容.xml version="1.0" encoding="ISO-8859-1"?>-- Copyright 2004 camry.wu@gmail.com This program is

Explorerwen--Javahome 906

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

使用 ant 让你愉快编程(2)

[首页, 上一页, 下一页]2  通用配置摘要: 介绍 build_common.xml, common.xml 及 usage.txtbuild_common.xml    <!-   Copyright 2004 camry.wu@gmail.com     This program is free software; you can r

寻道客 5953

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

使用 ant 让你愉快编程(6)

[首页, 上一页, 下一页]6  与 cvs 合作摘要: 简要介绍如何与 cvs 一起合作.很多人用 cvs 作为版本控制工具, 但是它有一点点麻烦, 就是 commit 的时候没有 -r 选项, 不能把子目录数据也传上去. 不要紧, 可以用 ant 把这个缺口补上, 只不过需要先做一些准备工作.在 work/common/template/src/ 目录下有一

寻道客 3983

使用 ant 让你愉快编程(5)

[首页, 上一页, 下一页]5  与 tomcat 合作摘要: 介绍 build_tomcat.xml, tomcat.xml现在介绍一下如何与 tomcat 一起合作.java 的程序很大部分都涉及 web, 自己测试时使用 tomcat 还是很方便的.在 work/common/ 下的 build_tomcat.xml, tomcat.xml 这两个文件

寻道客 4536

使用 ant 让你愉快编程(4)

摘要: checkstyle_checks.xml, java.header现在已经有了目录结构, 有了 build_common.xml, common.xml, usage.txt,build.xml, build.properties 等文件, 已经可以完成大部分任务了.现在介绍如何进行辅助任务, 比如代码检查.代码检查在 build_common.xml 中已经有这个任务了, 但是还需要两

Explorerwen--Javahome 913
上一篇: 在Eclipse中使用JUnit(翻译)
godding
博客等级 码龄25年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址