如何使用gradle构建工具打包groovy脚本成jar文件

如何使用gradle构建工具打包groovy脚本成jar文件,第1张

准备工作安装 gradle, groovy。

要使用gradle的groovy plugin 来打包groovy 脚本,项目结构。

目大弊念录 含义

src/main/java Java 代码

src/main/resources Java需要的资源文件

src/main/groovy Groovy代码,也可以包含Java代码

src/test/javaJava 测试代码

src/test/resources测试需要的资源文件

src/test/groovyGroovy测试需要的资源文件

src/滚困sourceSet/java Java代码源

src/sourceSet/resources 资源文件源

src/sourceSet/groovy Groovy代码源

我们只需要编译打包groovy脚本 所以只需要创建 src/main/groovy目录结构。例子:卜旁

gradle_groovy_archive项目 结构是:

gradle_groovy_archive

创建helloWorld.groovy脚本,代码如下:

package hello

println 'Gradle compile groovy'

创建Gradle构建文件:

apply plugin: 'groovy'

apply plugin: 'maven'

group = 'com.hello'

archiveBaseName = 'hello'

version = '0.1-SNAPSHOT'

defaultTasks 'clean', 'jar'

configurations {

deployerJars

}

repositories {

mavenCentral()

}

dependencies {

//使用本地groovy环境

groovy localGroovy()

//groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.6'

compile fileTree( dir: 'lib', include: ['*.jar'])

deployerJars 'org.apache.maven.wagon:wagon-webdav-jackrabbit:1.0-beta-7'

}

sourceSets {

main {

groovy {

srcDir 'src/main/groovy'

}

}

}

uploadArchives {

repositories.mavenDeployer {

uniqueVersion = false

configuration = configurations.deployerJars

repository(id : repositoryId, url : repositoryUrl) {

authentication (userName : 'deployment', password : 'deployment')

proxy()

}

}

}

创建构建文件属性文件:

//根据不同情况修改

repositoryId=ND

repositoryUrl=ND

systemProp.http.proxyHost=ND

systemProp.http.proxyPort=8080

systemProp.http.proxyUser=ND

systemProp.http.proxyPassword=ND

在命令行中 敲 gradle 运行,会自动运行 defaultTasks,clean 和 jar,会把 所有groovy下的脚本打成jar包。

xml内容

<emails>

<email id= ′>

<subject>hello world</subject>

<sender></sender>

<receivedDate>: : </receivedDate>

</email>

<email id= ′>

<subject>hello world demo </subject>

<清搏轿sender></sender>

<receivedDate>: : </receivedDate>

</email>

<email id= ′>

<subject>hello world demo </subject>

<sender></sender>

<receivedDate>: : </receivedDate>

</email>

</emails>

java解析实现

import w c dom Document

import w c dom Node

import w c dom NodeList

 银渣 import xml sax SAXException

import javax xml parsers DocumentBuilder

import javax xml parsers DocumentBuilderFactory

import javax xml parsers ParserConfigurationException

import java io IOException

import java io InputStream

public class EmailsDemo {

public static void main(String[] args)

{

DocumentBuilderFactory factory = DocumentBuilderFactory newInstance()

try {

DocumentBuilder builder = factory newDocumentBuilder()

InputStream is = EmailsDemo class getResourceAsStream( /emails xml )

Document document = builder parse(is)

NodeList nodes = document getElementsByTagName( email )

for (int i = i <nodes getLength()i++)

{

Node node = em(i)

Node id = node getAttributes() getNamedItem( id )

System out println( id = + id getTextContent())

}

} catch (ParserConfigurationException e) {

e printStackTrace()

} catch (SAXException e) {

e printStackTrace()

} catch (IOException e) {

答肆e printStackTrace()

}

}

}

groovy解析实现

def emails = new XmlParser() parse( emails xml )

emails email each {

println ${it @id} ${it subject text()}

}

lishixinzhi/Article/program/Java/hx/201311/26896


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12299454.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存