I need to start a WireMock server from a gradle task. In java I would do something like this:
WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(8089));
How can I create this wireMock Server using gradle task ?
It can be done in exactly the same way. Here You've a working example:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.tomakehurst:wiremock:1.52'
}
}
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig
import com.github.tomakehurst.wiremock.*
apply plugin: 'java'
task someTask {
doLast {
WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(8089))
println wireMockServer
}
}
If an external library is to be used in build.gradle itself it should be added as a dependency to buildscript block.
Related
I have a multi-project, and a few of its subprojects use the Gradle Plugin for Node.
./subproject/build.gradle:
// GitHub: https://github.com/node-gradle/gradle-node-plugin
plugins {
id "com.github.node-gradle.node" version "3.4.0"
}
// ...
I would now like to configure all of these subprojects from the root build.gradle.
./build.gradle
pluginManager.withPlugin('what.do.i.put.here') {
node {
download = true
}
// ...
}
Now my question: What do I have to put in as parameter for withPlugin? Where do I find the "official" plugin name to use?
Instead of using the pluginManager I found the following way to achieve this:
// This is executed on all subprojects
subprojects {
// This is executed on all subprojects that use the NodeJS plugin (https://github.com/node-gradle/gradle-node-plugin - npm_<...> (e.g., npm_install))
plugins.withId('com.github.node-gradle.node') {
node {
download = true
}
It uses plugins.withId inside of subprojects, using the ID also specified in the plugins section: com.github.node-gradle.node
I need to start a groovy script during the gradle-run-task after the compileGroovy-task, in order to create some resources. Therefore I have made the following build.gradle-file:
apply plugin: 'groovy'
apply plugin:'application'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.3'
}
task myTask << {
new GroovyShell().run(file('/src/someScript.groovy'))
}
myTask.mustRunAfter(compileGroovy)
myTask.dependsOn(compileGroovy)
Problem is: myTask is never executed when I execute the run-task.
How can I let myTask be executed after compileGroovy-task, which is nested inside run-task?
Try adding:
compileGroovy.finalizedBy(myTask)
It should solve the problem.
New to Groovy, gradle. I want to extend the following task (part of java plugin):
jar {
from { configurations.runtime.collect { zipTree(it) } }
manifest.attributes( 'Implementation-Title': archivesBaseName,
'Implementation-Version': version,
'Main-Class': mainClassName,
'Built-By': POM_DEVELOPER_NAME
)
}
The extended task will have
archiveName = 'PJ-latest.jar'
destinationDir = project.getRootDir()
In typical Java I would have called super but I'm not sure how I can do it in gradle. My best try is to extend the Jar class add the same parameters as jar has and also the dependencies:
task assembleCompiler(type: Jar){
dependsOn compileJava, processResources, classes
archiveName = 'PJ-latest.jar'
destinationDir = project.getRootDir()
from { configurations.runtime.collect { zipTree(it) } }
manifest.attributes( 'Implementation-Title': archivesBaseName,
'Implementation-Version': version,
'Main-Class': mainClassName,
'Built-By': POM_DEVELOPER_NAME
)
}
You'll have to configure the second task separately, like you already did. If you want to share this code, turn it into a plugin.
I have a parent gradle script with some common configuration, but I need to override some values. For this values I define extra properties. Next, in a project I apply the parent file, but I'm not able to override the the value. Here is what I try to do, but id doesn't work.
Parent gradle script (parent.gradle)
apply plugin: 'maven'
ext {
artifact = "test"
}
uploadArchives {
repositories.mavenDeployer {
repository(url: 'someUrl') {
authentication(userName: 'username', password: 'password')
}
pom.project {
artifactId artifact
}
}
}
Project gradle script (build.gradle)
apply from: 'parent.gradle'
ext {
artifact = "parent-gradle"
}
...
In the documentation I did not find any reference on how to do this.
Any idea on how I can do this?
Thanks Peter. Moving the ext statement before apply in the build.gradle, and removing it from parent.gradle solved my problem.
I have a gradle project that has java applications as well as android applications.
root/
build.gradle
settings.gradle
java1/
java2/
android1/
android2/
java3/
etc.
What is the best practice for structuring my build script? I am a total gradle novice and am migrating the project from maven to gradle.
I wanted to do something instead of
configure(subprojects) {}
to apply plugins and other specific things.
such as
configure(java1, java2, java3) { // java specifics }
configure(android1, android2) { // android specifics }
I am probably approaching this from the wrong way.
More explicitly I need to apply the plugin java only for the java projects and the android plugin for the android projects.
configure(subprojects.findAll {it.name == "java1" || it.name == "java2"}) {
Under the filtering section in the guide
Hope this helps someone else out.
There are multiple ways, depending on what you want... Some examples:
// Configures just project java1
project(":java1") { ... }
// Configures projects java1 and java2
["java1","java2"].each { name ->
project(":$name") { ... }
}
You can use normal groovy to find/iterate over all the projects.
Another option:
configure([ project(':sub1'), project(':sub2'), ... ]) {
...
}
The shortest and easiest option:
configure(subprojects.findAll()) {
if (it.name.equals('MY_PROJECT')) {
...
} else {
...
}
}
Another approach...
In the settings.gradle you do define your projects like this:
gradle.ext.javaProjects=[]
gradle.ext.androidProjects=[]
javaProject('java1')
javaProject('java2')
javaProject('java3')
androidProject('android1')
androidProject('android2')
def javaProject(String name) {
gradle.ext.javaProjects.add(name)
include ":$name"
}
def androidProject(String name) {
gradle.ext.androidProjects.add(name)
include ":$name"
}
Now you can reference those in your root build.gradle:
def javaProjects = subprojects.findAll {gradle.ext.javaProjects.contains(it.name)};
def androidProjects = subprojects.findAll {gradle.ext.javaProjects.contains(it.name)};
configure(javaProjects) {
...
}
configure(androidProjects) {
...
}
Maybe thats overkill... but i usually have the project definition method in my settings.gradle anyway. E.g. if you want to put all java projects under a java folder you could define things like this:
def javaProject(String name) {
gradle.ext.javaProjects.add(name)
include ":$name"
project(":$name").projectDir = file('java/'+ name)
}