简单的生活,更少的期待,更多的付出。

使用ant打包maven项目

问题很奇怪,估计没啥雷同的了。
简单来说,要求必须用ant打包maven项目。
最终只需一个war包,其他项目打成jar包,放在war包中。

在每个maven子项目中,建立一个用于ant buildfile的xml。

1
<!-- 源文件目录 -->
2
<property name="src.dir.core" value="src/main/java" />
3
<!-- 编译jar文件目录 -->
4
<property name="jar.dir.core" value="../compile_lib" />
5
<property name="j2eejar.dir.core" value="../j2ee_lib" />
6
<!-- WEB文件目录 -->
7
<property name="web.dir.core" value="src/main/webapp" />
8
<!-- config || output address -->
9
<property name="config.dir.core" value="src/main/resource" />
10
<!-- jar文件存放目录 -->
11
<property name="dist.dir.core" value="../compile_lib" />
12
<!-- 编译文件存放目录 -->
13
<property name="build.dir.core" value="build" />
14
<!-- 生成应用名 -->
15
<property name="webapp.name.core" value="jar包文件名" />
16
<!-- 编译环境 -->
17
<property environment="env" />
18
<path id="classpath.core">
19
	<fileset dir="${jar.dir.core}">
20
		<include name="*.jar" />
21
	</fileset>
22
	<fileset dir="${j2eejar.dir.core}">
23
		<include name="*.jar" />
24
	</fileset>
25
	<pathelement path="${build.dir.core}" />
26
</path>
27
<target name="RUN_PACKAGE" description="Packages app as JAR">
28
	<echo message="正在清空编译目录,生成目录..."/>
29
	<delete dir="${build.dir.core}/classes"/>
30
	<delete file="${dist.dir.core}/${webapp.name.core}.jar"/>
31
	<echo message="正在创建编译目录...."/>
32
	<mkdir dir="${build.dir.core}/classes" />
33
	<javac destdir="${build.dir.core}/classes" debug="false" deprecation="false" optimize="true" failonerror="true">
34
		<compilerarg line="-encoding UTF-8"/>
35
		<src path="${src.dir.core}"/>
36
		<classpath refid="classpath.core" />
37
	</javac>
38
	<echo message="正在编译文件目录...."/>
39
	<!-- Copy config files to ${build.dir.core}/classes -->
40
	<antcall target="DELETE-PAGES"/>
41
	<antcall target="COPY-PAGER"/>
42
	<antcall target="COPY-TLD"/>
43
	<echo message="正在生成JAR包..."/>
44
	<!-- 打jar包时,导入哪些配置,不导入哪些配置-->
45
	<jar destfile="${dist.dir.core}/${webapp.name.core}.jar" basedir="${build.dir.core}/classes" >
46
		<fileset dir="${src.dir.core}">
47
			<include name="**/*.properties" />
48
			<include name="**/*.xml" />
49
			<include name="**/*.tld" />
50
			<include name="**/*.ftl" />
51
			<include name="**/*.jasper" />
52
			<include name="**/*.jrxml" />
53
			<exclude name="**/*.java" />
54
		</fileset>	
55
	</jar>	
56
	<echo message="清理相关编译文件..."/>
57
	<delete dir="${build.dir.core}/classes"/>
58
</target>
59
<!--删除指定路径下文件及目录-->
60
<target name="DELETE-PAGES" >
61
	<property name="delete_path_core" value="${build.dir.core}\classes\someproject\"/>
62
	<echo>----------------------------------START------------------------------------</echo>
63
	<echo>开始删除${delete_path_core}下文件</echo>
64
	<delete dir="${delete_path_core}"/>  
65
	<echo>删除完成</echo>
66
	<echo>-----------------------------------END-----------------------------------</echo>
67
</target>
68
<!--COPY PAGES到指定路径下-->
69
<target name="COPY-PAGER" >
70
	<!--调用删除-->
71
	<antcall target="DELETE-PAGES">
72
			<param name="delete_path_core" value="${build.dir.core}\classes\someproject\resource\"/>
73
	</antcall>
74
	<!--webapp 页面路径 -->
75
	<property name="pager_path_core" location="${web.dir.core}\pages\ylink\someproject\" />
76
	<property name="script_path_core" location="${web.dir.core}\script\someproject\" />
77
	<property name="style_path_core" location="${web.dir.core}\style\" />
78
	<property name="to_pager_path_core" location="${build.dir.core}\classes\someproject\resource\pages" />
79
	<property name="to_script_path_core" location="${build.dir.core}\classes\someproject\resource\script\someproject" />
80
	<property name="to_style_path_core" location="${build.dir.core}\classes\someproject\resource\style" />
81
	<echo>-----------------------------------START-----------------------------------</echo>
82
	<echo>(${pager_path_core})----------->(${to_pager_path_core})</echo>
83
	<echo>开始拷贝JSP页面</echo>
84
	<copy todir="${to_pager_path_core}">
85
		<fileset dir="${pager_path_core}">
86
			<exclude name="someproject2/**" />
87
		</fileset>
88
	</copy>
89
	<echo>JSP页面拷贝结束</echo>
90
	<echo>开始拷贝scirpt</echo>
91
	<copy todir="${to_script_path_core}">
92
		<fileset dir="${script_path_core}">
93
			<include name="*/**" />
94
		</fileset>
95
	</copy>
96
	<echo>script脚本拷贝结束</echo>
97
	<echo>开始拷贝style</echo>
98
	<copy todir="${to_style_path_core}">
99
		<fileset dir="${style_path_core}">
100
			<include name="*/**" />
101
		</fileset>
102
	</copy>
103
	<echo>style样式拷贝结束</echo>	
104
	<echo>开始拷贝resource文件</echo>
105
	<copy todir="${build.dir.core}/classes">
106
		<fileset dir="${config.dir.core}">
107
			<include name="*/**" />
108
			<exclude name="**/jdbc*.properties" />
109
		    <exclude name="**/mqconfig*.properties" />
110
		    <exclude name="**/messages*.properties" />
111
		    <exclude name="**/system*.properties" />
112
		    <exclude name="**/cache*.properties" />
113
		    <exclude name="**/report*.properties" />
114
		    <exclude name="**/upload*.properties" />
115
			<exclude name="**/webinit-extend*.properties" />
116
		    <exclude name="**/logback*.xml" />
117
		    <exclude name="**/struts*.xml" />
118
		    <exclude name="**/layout*.xml" />
119
		</fileset>
120
	</copy>
121
	<echo>resource文件拷贝结束</echo>	
122
	<echo>-------------------------------------END---------------------------------</echo>
123
</target>
124
<!--COPY TLD到指定路径下-->
125
<target name="COPY-TLD" >
126
	<!--调用删除-->
127
	<antcall target="DELETE-PAGES">
128
            <param name="delete_path_core" value="${build.dir.core}\classes\someproject\tlds\"/>
129
    </antcall>
130
	<property name="pager_path_core" location="${web.dir.core}\WEB-INF\tlds\" />
131
	<property name="to_path_core" location="${build.dir.core}\classes\someproject\tlds\" />
132
	<echo>-----------------------------------START-----------------------------------</echo>
133
	<echo>(${pager_path_core})----------->(${to_path_core})</echo>
134
	<echo>开始拷贝TLD文件</echo>
135
	<copy todir="${to_path_core}">
136
        <fileset dir="${pager_path_core}">
137
          	<include name="*/**" />
138
        </fileset>
139
    </copy>
140
	<echo>TLD文件拷贝结束</echo>
141
	<echo>-------------------------------------END---------------------------------</echo>
142
</target>

打war包xml可运行其他jar包xml,然后打war包。

互相依赖的关系,没有解决。如,A依赖B,B依赖A
作弊的方法是,已有A/Bjar包。
唉,这方法其实没啥记录的必要。熟悉下ant和maven好了。

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="项目名" basedir="." default="main">
    <!-- 源文件目录 -->
    <property name="src.dir" value="src/main/java" />
    <!-- 编译jar文件目录 -->
    <property name="jar.dir" value="../compile_lib" />
    <!-- WEB文件目录 -->
    <property name="web.dir" value="src/main/webapp" />
    <!-- config || output address -->
    <property name="config.dev.dir" value="../config/dev" />
    <property name="config.dir" value="src/main/resource" />
    <!-- jar文件存放目录 -->
    <property name="dist.dir" value="../dist" />
    <property name="dist.dev.dir" value="../dist/dev" />
    <!-- 编译文件存放目录 -->
    <property name="build.dir" value="build" />
    <!-- 生成应用名 -->
    <property name="webapp.name" value="项目名" />
    <property environment="env" />
    <path id="classpath">
        <fileset dir="${jar.dir}">
            <include name="*.jar" />
        </fileset>
        <pathelement path="${build.dir}" />
    </path>    
    <!-- 构建子工程XXXX任务 -->
    <target name="子工程">
        <echo message="构建子工程「XXXX」!"/>
        <ant  dir="../XXXX"/>
    </target>    
    <!-- 生成war包 命令: ant -buildfile build.xml -->
    <target name="RUN_PACKAGE" depends="子工程,子工程2" description="Packages app as WAR">
        <echo message="正在清空编译目录,生成目录..."/>
        <delete dir="${build.dir}/classes"/>
        <delete dir="${build.dir}/lib"/>
        <delete file="${dist.dir}/${webapp.name}.war"/>
        <echo message="正在创建编译目录...."/>
        <mkdir dir="${build.dir}/classes" />
        <javac destdir="${build.dir}/classes" debug="false" deprecation="false" optimize="true" failonerror="true">
            <compilerarg line="-encoding UTF-8"/>
            <src path="${src.dir}"/>
            <classpath refid="classpath" />
        </javac>
        <echo message="正在编译文件目录...."/>
        <!-- Copy config files to ${build.dir}/classes -->
        <copy todir="${build.dir}/classes">
            <fileset dir="${config.dir}" includes="**/*.*" />
        </copy>
        <copy todir="${build.dir}/classes">
            <fileset dir="${src.dir}" includes="**/*.*" excludes="**/*.java" />
        </copy>
        <copy todir="${build.dir}/lib">
            <fileset dir="${jar.dir}" includes="**/*.*" />
        </copy>
        <echo message="正在生成WAR包..."/>
        <war destfile="${dist.dir}/${webapp.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
            <classes dir="${build.dir}/classes" />
            <lib dir="${build.dir}/lib" />
            <fileset dir="${web.dir}">
                <include name="**/*.*" />
                <exclude name="**/classes/**/*.*"/>
                <exclude name="**/web.xml" />
            </fileset>
        </war>
        <echo message="清理相关编译文件..."/>
        <delete dir="${build.dir}/classes"/>
        <delete dir="${build.dir}/lib"/>
    </target>
    <target name="DEV" depends="子工程,子工程2" description="Packages app as WAR  DEV">
        <echo message="正在清空编译目录,生成目录..."/>
        <delete dir="${build.dir}/classes"/>
        <delete dir="${build.dir}/lib"/>
        <delete file="${dist.dev.dir}/${webapp.name}.war"/>
        <echo message="正在创建编译目录...."/>
        <mkdir dir="${build.dir}/classes" />
        <javac destdir="${build.dir}/classes" debug="false" deprecation="false" optimize="true" failonerror="true">
            <compilerarg line="-encoding UTF-8"/>
            <src path="${src.dir}"/>
            <classpath refid="classpath" />
        </javac>
        <echo message="正在编译文件目录...."/>
        <!-- Copy config files to ${build.dir}/classes -->
        <copy todir="${build.dir}/classes">
            <fileset dir="${config.dev.dir}" includes="**/*.*"/>
        </copy>
        <copy todir="${build.dir}/classes">
            <fileset dir="${config.dir}" includes="**/*.*" />
        </copy>
        <copy todir="${build.dir}/classes">
            <fileset dir="${src.dir}" includes="**/*.*" excludes="**/*.java" />
        </copy>
        <copy todir="${build.dir}/lib">
            <fileset dir="${jar.dir}" includes="**/*.*" />
        </copy>
        <echo message="正在生成WAR包..."/>
        <war destfile="${dist.dev.dir}/${webapp.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
            <classes dir="${build.dir}/classes" />
            <lib dir="${build.dir}/lib" />
            <fileset dir="${web.dir}">
                <include name="**/*.*" />
                <exclude name="**/classes/**/*.*"/>
                <exclude name="**/web.xml" />
            </fileset>
        </war>
        <echo message="清理相关编译文件..."/>
        <delete dir="${build.dir}/classes"/>
        <delete dir="${build.dir}/lib"/>
    </target>
    <!-- 默认任务 -->
    <target name="main" depends="RUN_PACKAGE"/>
</project>
-------------本文结束感谢您的阅读-------------
谢谢大爷打赏,常来玩啊