build.gradle
file. We write: task hello << { println 'Hello world!' }
>gradle -q hello
Hello world!
task upper << { String someString = 'mY_nAmE' println "Original: " + someString println "Upper case: " + someString.toUpperCase() 4.times { print "$it " } }
>gradle -q upper
Original: mY_nAmE
Upper case: MY_NAME
0 1 2 3
apply plugin: 'java' version = '1.0' repositories { mavenCentral() } dependencies { compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.7' }
/ projectAlpha / src / test / main / java / my / own / code / spring / db / plugin / auth
>gradle build
projectAlpha/build/libs
directory will projectAlpha-1.0.jar
. All in full accordance with the agreements. Maven would do the same. task sourcesJar(type: Jar) { appendix = 'sources' from sourceSets.main.allJava } task testJar(type: Jar) { appendix = 'test' from sourceSets.test.classes } jar { exclude 'my/spring/**' } task springDbJar(type: Jar) { appendix = 'spring-db' from sourceSets.main.classes include 'my/spring/db/**' } task springAuthJar(type: Jar) { appendix = 'spring-auth' from sourceSets.main.classes include 'my/spring/auth/**' } task springPluginJar(type: Jar) { appendix = 'spring-plugin' from sourceSets.main.classes include 'my/spring/plugin/**' }
>gradle assemble
projectAlpha>dir /b build\libs
projectAlpha-1.0.jar
projectAlpha-sources-1.0.jar
projectAlpha-spring-auth-1.0.jar
projectAlpha-spring-db-1.0.jar
projectAlpha-spring-plugin-1.0.jar
projectAlpha-test-1.0.jar
sourcesJar
and testJar
. To describe the contents of the archive are already familiar to you source sets. Another attribute is appendix
, which, as you might guess, will be included in the archive name after the version.jar
task (it is defined in the plugin) so that the main archive does not get classes from certain packages.assemble
task, the assembly system selected all the tasks forming the archives (Zip, Jar ..) and performed them. Pre-processing dependencies on source sets and compiling the necessary classes, as in the previous article. tasks.withType(Jar).matching { task -> task.archiveName.contains('spring') }.allObjects { task -> task.manifest { attributes demo: 'habr.ru' } }
projectAlpha>gradle assemble
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:sourcesJar UP-TO-DATE
:springAuthJar
:springDbJar
:springPluginJar
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:testJar UP-TO-DATE
:assemble
Manifest-Version: 1.0
demo: habr.ru
def allSpringJars = tasks.withType(Jar).matching { task -> task.archiveName.contains('spring') } allSpringJars.allObjects { task -> configure(task) { manifest { attributes demo: 'habr.ru' } doLast { ant.checksum(file: archivePath, todir: archivePath.parentFile) } } } task springJars(dependsOn: allSpringJars)
projectAlpha>gradle clean springJars
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:springAuthJar
:springDbJar
:springPluginJar
:springJars
BUILD SUCCESSFUL
Total time: 5.015 secs
c:\Work\Gradle\tasksAreCode\projectAlpha>dir /b build\libs
projectAlpha-spring-auth-1.0.jar
projectAlpha-spring-auth-1.0.jar.MD5
projectAlpha-spring-db-1.0.jar
projectAlpha-spring-db-1.0.jar.MD5
projectAlpha-spring-plugin-1.0.jar
projectAlpha-spring-plugin-1.0.jar.MD5
Source: https://habr.com/ru/post/107558/
All Articles