Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gradle.pdf
Скачиваний:
9
Добавлен:
24.03.2015
Размер:
1.4 Mб
Скачать

“Repositories”.

8.6. Publishing artifacts

Dependency configurations are also used to publish files.[3] We call these files publication artifacts, or usually just artifacts.

The plugins do a pretty good job of defining the artifacts of a project, so you usually don't need t do anything special to tell Gradle what needs to be published. However, you do need to tell Gradle where to publish the artifacts. You do this by attaching repositories to the uploadArchives task. Here's an example of publishing to a remote Ivy repository:

Example 8.8. Publishing to an Ivy repository

build.gradle

uploadArchives { repositories {

ivy { credentials {

username "username" password "pw"

}

url "http://repo.mycompany.com"

}

}

}

Now, when you run gradle uploadArchives, Gradle will build and upload your Jar. Gradle will also generate and upload an ivy.xml as well.

You can also publish to Maven repositories. The syntax is slightly different.[4] Note that you also need to apply the Maven plugin in order to publish to a Maven repository. In this instance, Gradle will generate and upload a pom.xml.

Example 8.9. Publishing to a Maven repository

build.gradle

apply plugin: 'maven'

uploadArchives { repositories {

mavenDeployer {

repository(url: "file://localhost/tmp/myRepo/")

}

}

}

To find out more about publication, have a look at Chapter 44, Publishing artifacts.

Page 49 of 343

8.7. Where to next?

For all the details of dependency resolution, see Chapter 43, Dependency Management, and for artifact publication see Chapter 44, Publishing artifacts.

If you are interested in the DSL elements mentioned here, have a look at

Project.configurations{}, Project.repositories{} and Project.dependencies{}

.

Otherwise, continue on to some of the other tutorials.

[3] We think this is confusing, and we are gradually teasing apart the two concepts in the Gradle DSL.

[4] We are working to make the syntax consistent for resolving from and publishing to Maven repositories.

Page 50 of 343

9

Groovy Quickstart

To build a Groovy project, you use the Groovy plugin. This plugin extends the Java plugin to add Groovy compilation capabilities to your project. Your project can contain Groovy source code, Java source code, or a mix of the two. In every other respect, a Groovy project is identical to a Java project, which we have already seen in Chapter 7, Java Quickstart.

9.1. A basic Groovy project

Let's look at an example. To use the Groovy plugin, add the following to your build file:

Example 9.1. Groovy plugin

build.gradle

apply plugin: 'groovy'

Note: The code for this example can be found at samples/groovy/quickstart which is in both the binary and source distributions of Gradle.

This will also apply the Java plugin to the project, if it has not already been applied. The Groovy plugin extends the compile task to look for source files in directory src/main/groovy, and the c task to look for test source files in directorysrc/test/groovy. The compile tasks use joint compilation for these directories, which means they can contain a mixture of java and groovy source files.

To use the groovy compilation tasks, you must also declare the Groovy version to use and where to find the Groovy libraries. You do this by adding a dependency to the groovy configuration. The configuration inherits this dependency, so the groovy libraries will be included in classpath when compiling Groovy and Java source. For our sample, we will use Groovy 1.6.0 from the public Maven repository:

Page 51 of 343

Example 9.2. Dependency on Groovy 1.6.0

build.gradle

repositories { mavenCentral()

}

dependencies {

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

}

Here is our complete build file:

Example 9.3. Groovy example - complete build file

build.gradle

apply plugin: 'eclipse' apply plugin: 'groovy'

repositories { mavenCentral()

}

dependencies {

groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.7.10' testCompile group: 'junit', name: 'junit', version: '4.8.2'

}

Running gradle build will compile, test and JAR your project.

9.2. Summary

This chapter describes a very simple Groovy project. Usually, a real project will require more than this. Because a Groovy project is a Java project, whatever you can do with a Java project, you can also do with a Groovy project.

You can find out more about the Groovy plugin in Chapter 24, The Groovy Plugin, and you can find more sample Groovy projects in the samples/groovy directory in the Gradle distribution.

Page 52 of 343

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]