且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从普通的Groovy转移到Gradle

更新时间:2023-12-05 14:39:34

紧急问题是您需要将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'
$ c>

可以表示为'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'
}