关闭

gradle 基本构建配置

标签: gradle
581人阅读 评论(0) 收藏 举报
分类:

参考:

http://blog.jobbole.com/84471/


自定义几个简单脚本

生成jar

build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'


repositories {
    mavenCentral()
}

dependencies { 
   testCompile group: 'junit', name: 'junit', version: '4.+'
}

jar {
manifest {
attributes 'Main-Class': 'idet.rui.user.Hello'
}
}





package idet.rui.user;

public class Hello {

	public static void main(String[] args) {
		System.out.println("hello world");
		
		
	}

}

java -jar idet.jar  会输出


多项目

settings.gradle

include "app", "core","rui-test"

build.gradle

subprojects {
	apply plugin: 'java'
	apply plugin: 'eclipse'
    repositories {
         mavenCentral()
    }
}

allprojects {
    //Add configuration here
}


project(':rui-test') {
	dependencies{
		 compile 'junit:junit:4.11'
	}
}

project(':core') {
	dependencies{
		 testCompile project(':rui-test') 
	}
}

project(':app') {
	dependencies{
	    testCompile project(':rui-test') 
	    compile 'log4j:log4j:1.2.17'     	
	}
}


------------------------------------------------------------------

一个简单项目 gradle和jetty结合

项目github   https://github.com/liangrui1988/gradleSimpleDome

def SpringVersion = '4.0.1.RELEASE'
def JettyVersion = '8.1.14.v20131031'

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'war'


// gradle 自已用的
buildscript {
        repositories {
		     maven { url "http://10.12.0.110:8081/nexus/content/groups/public/" }
             mavenCentral()
        }
        dependencies {
            // classpath "net.saliman:gradle-cobertura-plugin:2.2.2"
        }
}

// JVM 版本号要求
sourceCompatibility = 1.7
targetCompatibility = 1.7  

repositories {
    maven { url "http://10.12.0.110:8081/nexus/content/groups/public/" }
    mavenCentral()
}


[compileJava, compileTestJava]*.options*.encoding = "UTF-8"


configurations {
     // 所有需要忽略的包定义在此
	all*.exclude group: 'commons-logging', module: 'commons-logging'
}
	
//最终的 Jar 包名,如果没设置,默认为 [baseName]-[appendix]-[version]-[classifier].[extension]
archivesBaseName = "gradle-test-6.6.6.jar"
	
dependencies { 
   testCompile group: 'junit', name: 'junit', version: '4.+'    
   compile 'org.slf4j:slf4j-api:1.7.5'
   compile 'org.slf4j:jcl-over-slf4j:1.7.5'
   compile 'ch.qos.logback:logback-classic:1.0.13'
   compile 'org.apache.commons:commons-lang3:3.1'
   compile 'org.apache.httpcomponents:httpclient:4.3.4'
   compile 'org.apache.httpcomponents:httpmime:4.3.4'
   compile "org.eclipse.jetty:jetty-jsp:${JettyVersion}"
   compile "org.eclipse.jetty:jetty-webapp:${JettyVersion}"
   compile "org.eclipse.jetty:jetty-servlet:${JettyVersion}"
	compile "javax.servlet:javax.servlet-api:3.1.0"
	compile "javax.servlet.jsp:jsp-api:2.2.1-b03"
	compile "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1" 
}

//Java 插件已经向项目添加了 compileJava任务和processResources任务,并且配置了这两个任务的destinationDir属性
// 更改插件的默认设置
processResources << {
        file("$destinationDir/version.properties").text = 'version=' + project.version
    }

task copyLib(type: Sync, dependsOn: jar) {
		from jar.archivePath
		from configurations.compile
		into 'build/deploy'
	}
	
	task copyWebapp(type: Sync) {
		from 'src/main/webapp'
		into 'build/deploy/webapp'
	}
	
//task deploy(type: Sync, dependsOn: jar) 
task deploy(dependsOn: [copyLib,copyWebapp])



 // 显示当前项目下所有用于 compile 的 jar.
task listJars(description: 'Display all compile jars.') << {
    configurations.compile.each { File file -> println file.name }
}





package idet.rui.user;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.ExecutorThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;

public class SimpleMain {

	public static void main(String[] args) throws Exception {
		Server server = new Server();
		SelectChannelConnector connector = new SelectChannelConnector();

		// 设置线程
		server.setThreadPool(new ExecutorThreadPool());
		// 端口
		connector.setPort(9808);
		// 设置handler
		//项目目录名/src/main/webapp
		//lib/webapp
		server.setHandler(new WebAppContext("src/main/webapp", "/"));
		// 连接器
		server.addConnector(connector);
		server.start();
	}
}


访问页面成功


linux 上可以用sh 启动脚本

run.sh

#!/bin/bash
PATH_LIB=./lib
CLASSPATH=../etc

for jar in `ls $PATH_LIB/*.jar`
do
      CLASSPATH="$CLASSPATH:""$jar"
done

#        ARGS="$ARGS"" -Djava.rmi.server.hostname="
#        ARGS="$ARGS"" -Dcom.sun.management.jmxremote.port=8990"
#        ARGS="$ARGS"" -Dcom.sun.management.jmxremote.authenticate=false" 
#        ARGS="$ARGS"" -Dcom.sun.management.jmxremote.ssl=false" 
#        ARGS="$ARGS"" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8004" 

exec -a 项目目录 java -server -Xms64m -Xmx64m -XX:PermSize=16m -XX:MaxPermSize=128m -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:/srv/server/pro-web/work/gc.log $ARGS -classpath "$CLASSPATH" xx.xx.xx.WebMain 9809 >> work/stdout.log


在结合supervisor 管理启动脚本


不错的文档参考

http://my.oschina.net/zjzhai/blog/220028?fromerr=pFWeJprg

http://www.tuicool.com/articles/ZBJBjej

https://docs.gradle.org/current/dsl/org.gradle.api.initialization.Settings.html


task >>>>>>有时间补上


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:304875次
    • 积分:5352
    • 等级:
    • 排名:第5017名
    • 原创:166篇
    • 转载:40篇
    • 译文:33篇
    • 评论:90条
    文章分类
    最新评论
    student
    http://www.importnew.com/ http://my.oschina.net/abian/blog?catalog=322720 http://www.cnblogs.com/itech/ http://www.oschina.net/p/druid/