ant 打包文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>  
<project basedir="." default="jar" name="plugin">  
    <property environment="env"/>  
    <!-- JDK路径,根据自己机器上实际位置修改-->  
    <property name="jdk.home" value="${env.JAVA_HOME}"/>  
    <property name="libs" value="${basedir}/lib"/>  
    <property name="publicLibs" value="/Users/3dot141/Work/w_source/FR_11_Release/env/webroot/WEB-INF"/>  
    <property name="reportLibs" value="${basedir}/lib/report"/>  
    <property name="pluginLibs" value="/Users/3dot141/Work/w_source/FR_11_Release/plugins/WEB-INF/lib"/>  
    <property name="destLoc" value="."/>  
    <property name="classes" value="classes"/>  
    <xmlproperty file="${basedir}/plugin.xml"/>  
    <property name="current-version" value="${plugin.version}"/>  
  
    <!-- 插件版本-->  
    <property name="plugin-version" value="${current-version}"/>  
    <!-- 插件名字-->  
    <property name="plugin-name" value="${plugin.id}"/>  
    <property name="plugin-jar" value="fr-plugin-${plugin-name}-${plugin-version}.jar"/>  
  
    <target name="prepare">  
        <delete dir="${classes}"/>  
        <delete dir="fr-plugin-${plugin-name}-${plugin-version}"/>  
        <xmlproperty file="${basedir}/plugin.xml"/>  
        <delete dir="${destLoc}/${plugin-name}"/>  
    </target>    
    <path id="compile.classpath">  
        <fileset dir="${libs}">  
            <include name="**/*.jar"/>  
        </fileset>        
        <fileset dir="${publicLibs}">  
            <include name="**/*.jar"/>  
        </fileset>        
        <dirset dir="${publicLibs}">  
            <include name="classes"/>  
        </dirset>        
        <fileset dir="${pluginLibs}">  
            <include name="**/*.jar"/>  
        </fileset>    
	</path>   
	<patternset id="resources4Jar">  
        <exclude name=" **/.settings/** "/>  
        <exclude name=".classpath"/>  
        <exclude name=".project"/>  
  
        <exclude name="**/*.java"/>  
        <exclude name="**/*.db"/>  
        <exclude name="**/*.g"/>  
        <exclude name="**/package.html"/>  
    </patternset>    
    <target name="copy_resources">  
        <echo message="从${resources_from}拷贝图片,JS,CSS等资源文件"/>  
        <delete dir="tmp"/>  
        <copy todir="tmp">  
            <fileset dir="${resources_from}/src/main/resources">  
                <patternset refid="resources4Jar"/>  
            </fileset>        </copy>        <copy todir="${classes}">  
            <fileset dir="tmp"/>  
        </copy>        <delete dir="tmp"/>  
    </target>    <target name="compile_javas">  
        <echo message="编译${compile_files}下的Java文件"/>  
        <javac destdir="${classes}" debug="false" optimize="on" includeantruntime="false" source="${source_jdk_version}"  
               target="${target_jdk_version}"  
               fork="true" memoryMaximumSize="512m" listfiles="false"
               executable="${compile_jdk_version}/bin/javac">  
            <src path="${basedir}/src/main/java"/>  
            <exclude name=" **/.svn/** "/>  
            <exclude name=" **/.git/** "/>  
            <compilerarg line="-encoding UTF8"/>  
            <classpath refid="compile.classpath"/>  
        </javac><!--        <taskdef name="pretreatment" classname="com.fr.plugin.pack.PluginPretreatmentTask">-->  
<!--            <classpath refid="compile.classpath"/>-->  
<!--        </taskdef>-->  
<!--        <pretreatment baseDir="${basedir}"/>-->  
    </target>  
  
    <target name="jar_classes">  
        <echo message="打Jar包:${jar_name}"/>  
        <delete file="${basedir}/${jar_name}"/>  
        <echo message="解压druid"/>  
        <unjar dest="${classes}">  
            <fileset dir="${libs}" includes="druid.jar,icu4j-59.1.jar"/>  
        </unjar>        <jar jarfile="${basedir}/${jar_name}">  
            <fileset dir="${classes}">  
                <exclude name=" **/com/fr/test/** "/>  
            </fileset>        </jar>    </target>  
    <target name="super_jar" depends="prepare">  
        <antcall target="copy_resources">  
            <param name="resources_from" value="${basedir}"/>  
        </antcall>        <antcall target="compile_javas">  
            <param name="source_jdk_version" value="1.8"/>  
            <param name="target_jdk_version" value="1.8"/>  
            <param name="compile_jdk_version" value="\${jdk.home}"/>  
            <param name="compile_files" value="${basedir}/src"/>  
        </antcall>        <echo message="compile plugin success!"/>  
  
        <antcall target="jar_classes">  
            <param name="jar_name" value="${plugin-jar}"/>  
        </antcall>        <delete dir="${classes}"/>  
  
    </target>    <target name="jar" depends="super_jar">  
        <antcall target="zip"/>  
    </target>  
    <target name="zip">  
        <property name="plugin-folder" value="fr-plugin-${plugin-name}-${plugin-version}"/>  
        <echo message="----------zip files----------"/>  
        <mkdir dir="${plugin-folder}"/>  
        <copy todir="${plugin-folder}">  
            <fileset dir=".">  
                <include name="${plugin-jar}"/>  
                <include name="plugin.xml"/>  
            </fileset>        </copy>        <zip destfile="${basedir}/${plugin-folder}.zip" basedir=".">  
            <include name="${plugin-folder}/*.jar"/>  
            <include name="${plugin-folder}/plugin.xml"/>  
        </zip>        <!--xmlproperty file="/com/fr/plugin/performance/dependences/plugin.xml"/-->  
        <move file="${plugin-folder}.zip" todir="${destLoc}/${plugin.name}"/>  
    </target>
</project>

见上文的代码,主要是修改了

  • 编译路径

    • 增加了 classes
    • 将依赖的 JAR 包,统一打到一个 pluginLibs,然后依赖
    <path id="compile.classpath">  
    	<fileset dir="${libs}">  
    		<include name="**/*.jar"/>  
    	</fileset>        
    	<fileset dir="${publicLibs}">  
    		<include name="**/*.jar"/>  
    	</fileset>        
    	<dirset dir="${publicLibs}">  
    		<include name="classes"/>  
    	</dirset>        
    	<fileset dir="${pluginLibs}">  
    		<include name="**/*.jar"/>  
    	</fileset>    
    </path>
  • 编译目录

    • srcdir="${basedir}" 删除,原因见 Ant

gradle 打包文件

import org.gradle.plugins.ide.idea.model.IdeaLanguageLevel  
  
plugins {  
    id 'java'  
    id 'java-library'  
    id 'idea'  
    id 'java-gradle-plugin'  
    id 'com.fr.common' version '1.0-SNAPSHOT'  
}  
  
applyGlobalConfigPathIfExist()  
  
def frDevVersion = "DEV" + frVersion  
  
ext {  
    def pluginInfo = getPluginInfo()  
    pluginPre = "fine-plugin"  
    pluginName = pluginInfo.id  
    pluginVersion = pluginInfo.version  
  
    outputPath = prepareOutputPath(pluginName, pluginVersion)  
}  
  
/*读取plugin.xml中的version*/  
  
def getPluginInfo() {  
    def xmlFile = file("plugin.xml")  
    if (!xmlFile.exists()) {  
        return ["id": "none", "version": "1.0.0"]  
    }  
    def plugin = new XmlParser().parse(xmlFile)  
    def version = plugin.version[0].text()  
    def id = plugin.id[0].text()  
    return ["id": id, "version": version]  
}  
  
def prepareOutputPath(String pluginName, String pluginVersion) {  
    String plugins = "$projectDir/../env/webroot/WEB-INF/plugins/"  
    String prefix = "plugin-$pluginName-"  
    String pluginDir = prefix + pluginVersion  
    file(plugins).eachDir { File it ->  
        if (it.name.startsWith(prefix) && pluginDir != it.name) {  
            println "delete old plugin directory " + it.name  
            delete it  
        }  
    }  
    copy {  
        from "$projectDir/plugin.xml"  
        into plugins + pluginDir  
    }  
    return "${plugins}${pluginDir}/classes"  
}  
  
group = 'com.fr.plugin.parallel.loader'  
version = frDevVersion  
sourceCompatibility = JavaVersion.VERSION_1_8  
targetCompatibility = JavaVersion.VERSION_1_8  
  
idea {  
    module {  
        inheritOutputDirs = false  
        outputDir = file(outputPath)  
        languageLevel = new IdeaLanguageLevel(sourceCompatibility)  
        targetBytecodeVersion = targetCompatibility  
    }  
}  
  
tasks.withType(JavaCompile) {  
    options.encoding = "UTF-8"  
}  
  
repositories {  
    maven {  
        url 'https://mvn.fanruan.com/repository/maven-public/'  
    }  
    maven {  
        url 'https://maven.aliyun.com/repository/public/'  
    }  
    mavenCentral()  
    mavenLocal()  
}  
  
dependencies {  
    implementation 'com.fr.third.server:servlet-api:3.0'  
    implementation 'com.fr.third:fine-third:' + frVersion  
    implementation 'com.fr.activator:fine-activator:' + frVersion  
    implementation 'com.fr.datasource:fine-datasource:' + frVersion  
    implementation 'com.fr.core:fine-core:' + frDevVersion  
    implementation 'com.fr.report:engine-report:' + frDevVersion  
    implementation 'com.fr.report:engine-x:' + frDevVersion  
    implementation 'com.fr.decision:fine-decision:' + frDevVersion  
    implementation 'com.fr.design:design:' + frDevVersion  
    implementation 'org.apache.calcite:calcite-core:1.27.0'  
    implementation 'com.alibaba:druid:1.2.5'  
    testImplementation files("${System.properties['java.home']}/../lib/tools.jar")  
    testImplementation 'org.easymock:easymock:3.5.1'  
    testImplementation 'org.powermock:powermock-module-junit4:1.7.1'  
    testImplementation 'org.powermock:powermock-api-easymock:1.7.1'  
    testImplementation 'junit:junit:4.12'  
  
    //使用本地jar  
    implementation fileTree(dir: "$projectDir/lib", include: ['*.jar'])  
  
   compileOnly fileTree(dir: "lib", includes: ['*.jar'])  
   compileOnly fileTree(dir: "lib/report", includes: ['*.jar'])  
   testCompile fileTree(dir: "lib", includes: ['*.jar'])  
   testCompile fileTree(dir: "lib/report", includes: ['*.jar'])  
  
}
pluginManagement {  
    repositories {  
        mavenLocal()  
        maven {  
            allowInsecureProtocol = true  
            url 'http://mvn.finedevelop.com/repository/maven-public/'  
        }  
        gradlePluginPortal()  
    }  
}