从普通的Groovy转移到Gradle [英] Moving from plain Groovy to Gradle

查看:172
本文介绍了从普通的Groovy转移到Gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Groovy中有一个可用的小应用程序。目前我可以在命令行上测试它,但我需要将它与Jenkins服务器集成。因此,我曾想过将它与Gradle集成,以Jenkins可以读取的标准格式生成测试输出。 (请考虑我是Groovy,Gradle,Jenkins和JVM环境的新手)。

目前我所有的测试都在一个 MyTest 扩展了 GroovyTestCase 的类。我可以用一个小小的bash脚本运行它,比如

 #! / bin / bash 

DIR = $(cd$(dirname$ 0)&& pwd)
LIBS =$ DIR / lib / *

groovy -cp$ LIBSpath / to / MyTest.groovy

有多个在这个布局中我不喜欢的东西:




  • 首先,我想将测试分成多个类并将它们加入测试但是一个测试用例的优点是它可以在Groovy中自动运行

  • 其次,正如我所说的,我希望能够获得一份测试报告,供Jenkins

  • 第三,我不知道将所有需要的jar放入 lib 目录是否是一种好的做法 - 可能不是。



Gradle看起来正是我需要的工具。但是我发现的所有文档都假定了以前对Java,Ant,Maven和整个生态系统的了解。


创建一个基本步骤工作Gradle项目?

我按照建议这里,但我不明白如何声明依赖关系。例如,使用这个build.gradle

  apply插件:'groovy'

repositories {
mavenCentral()
}

依赖关系{
groovy组:'org.codehaus.groovy',名称:'groovy',版本:'1.8.0'
运行时组:'org.apache.log4j',名称:'log4j',版本:'1.2.0'
}

我得到错误

 无法解析类org.apache.log4j.Level 
@ line 5,column 1.
import org.apache.log4j.Level
^

如果我将存储库更改为

 存储库{
mavenCentral()
flatDir {dirs 'lib'}
}

得到 log4j $ lib 目录中,我得到

 一个问题发生了评估项目alfred。 
原因:无法在根项目'alfred'上为参数[build_3urmo05tgpv3e97u7h8ij47i3 $ _run_closure1_closure3 @ 64c7f7c4]找到方法flatDir()。


解决方案

紧急问题是您需要将log4j导入为一个编译时间依赖项。



解决方法是改变

 运行时组:'org.apache.log4j',名称:'log4j',版本:'1.2.0'

 编译组:'org.apache.log4j',名称:'log4j',版本:'1.2。 0'

基本上,运行时依赖使代码在运行时可用(用于反射,传递依赖等) ,但不是在编译时。如果您正在编译log4j,您需要编译时间。



编译时可用的所有东西在运行时自动提供。



接下来您要做的就是为您的maven样式的依赖项使用一个terser语法。

  group:'org.apache.log4j',名称:'log4j',版本:'1.2.0'

可表示为'org.apache.log4j:log4j:1.2.0'



 组:'org.codehaus.groovy',名称:'groovy',版本:'1.8.0'

可以表示为'org.codehaus.groovy:groovy:1.8.0' code>



放在一起,你的build.gradle文件应该是这样的:



<$












$ b $ {
groovy'org.codehaus.groovy:groovy:1.8.0'
compile'org.a pache.log4j:log4j:1.2.0'
}


I have a working little application made in Groovy. At the moment I can test it on the command line, but I need to integrate it with a Jenkins server. Hence I have thought to integrate it with Gradle to produce test output in a standard format that Jenkins can read. (Please, consider that I am new to Groovy, Gradle, Jenkins and the JVM environment in general).

At the moment all my tests live inside a single MyTest class that extends GroovyTestCase. I can run it with a little bash script like

#! /bin/bash

DIR=$( cd "$( dirname "$0" )" && pwd )
LIBS="$DIR/lib/*"

groovy -cp "$LIBS" path/to/MyTest.groovy

There are multiple things that I do not like in this layout:

  • First, I would like to separate the tests into multiple classes and join them into a test suite, but a test case had the advantage that it is automatically runnable in Groovy
  • Second, as I said, I would like to be able to obtain a test report to be consumed by Jenkins
  • Third, I do not know if putting all required jars into a lib directory is a good practice - probably not.

Gradle seems exactly the tool that I need. But all the documentation I find assumes previous knowledge of Java, Ant, Maven and the whole ecosystem.

What are the basic steps to create a working Gradle project?

I have reorganized the directory structure as suggested here, but I do not understand how to declare dependencies. For instance with this build.gradle

apply plugin: 'groovy'

repositories {
  mavenCentral()
}

dependencies {
  groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.0'
  runtime group: 'org.apache.log4j', name: 'log4j', version: '1.2.0'
}

I obtain the error

unable to resolve class org.apache.log4j.Level
 @ line 5, column 1.
   import org.apache.log4j.Level
   ^

If I change repositories to

repositories {
  mavenCentral()
  flatDir { dirs 'lib' }
}

to get log4j from my lib directory, I get

A problem occurred evaluating root project 'alfred'.
Cause: Could not find method flatDir() for arguments [build_3urmo05tgpv3e97u7h8ij47i3$_run_closure1_closure3@64c7f7c4] on root project 'alfred'.

解决方案

The immediate problem is that you need to import log4j as a compile-time dependency.

The fix is to change

runtime group: 'org.apache.log4j', name: 'log4j', version: '1.2.0'

to

compile group: 'org.apache.log4j', name: 'log4j', version: '1.2.0'

Basically, a runtime dependency makes code available at runtime (for reflection, transitive dependencies, etc), but not at compile time. If you're compiling against log4j, you'll need compile time.

Everything available at compile time is automatically available at runtime.

The next thing you'll want to do is use a terser syntax for your maven-style dependencies.

group: 'org.apache.log4j', name: 'log4j', version: '1.2.0'

can be expressed as 'org.apache.log4j:log4j:1.2.0'

and

group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.0'

can be expressed as 'org.codehaus.groovy:groovy:1.8.0'

put it all together, and your build.gradle file should look like this:

apply plugin: 'groovy'

repositories {
  mavenCentral()
}

dependencies {
  groovy 'org.codehaus.groovy:groovy:1.8.0'
  compile 'org.apache.log4j:log4j:1.2.0'
}

这篇关于从普通的Groovy转移到Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆