I am trying to migrate from ant to gradle to build my rpm-package. I found one plugin gradle-ospackage-plugin, but I am not able to understand how to use any of commands which I have in ant task: specFile, topDir, command, cleanbuildDir, failonError
Is it not possible to have them in gradle?
Update: Basically I am trying to replicate the following in gradle
<target name="myrpm">
<rpm specFile = "topdir"
topDir = "topdir" />
</target>
I was also looking into running ant tasks from gradle such as ant.echo(message: "hello).
But ant.rpm is not resolving.
A simple example that creates two RPMs. If you want a single RPM then use one task. Your build.gradle file should be like this.
plugins {
id 'java'
id "nebula.ospackage" version "3.2.0"
}
task one(type: Rpm) {
packageName = 'one-pack'
version = '1.0.0'
release = '1'
arch = I386
os = LINUX
from('./src/main/resources') {
into 'apps/lib/'
}
}
task two(type: Rpm) {
packageName = 'two-pack'
version = '2.2.0'
release = '2'
arch = I386
os = LINUX
}
apply plugin: 'nebula.ospackage'
group 'com.avishek'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'org.assertj:assertj-core:3.2.0'
}
Run the project with the following command:
./gradlew clean two one
Related
I have the following dockerfile.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
This is build.gradle.
buildscript {
ext {
springBootVersion = '2.0.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'docker'
bootJar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
group = 'com.pai'
version = '0.0.1-SNAPSHOT'
task buildDocker(type: Docker, dependsOn: build){
push = true
applicationName = jar.baseName
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'mysql:mysql-connector-java'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
Process sync of build.gradle is successful.
When I run the following command.
sudo ./gradlew build buildDocker
I get the following error message.
Execution failed for task ':buildDocker'.
> Docker execution failed
Command line [docker push com.pai/pai201808:0.0.1-SNAPSHOT] returned:
Get https://com.pai/v2/: dial tcp: lookup com.pai on 127.0.1.1:53: no such host
I think that this could be the issue of wrong import or invalid configurated dockerfile. How can I solve this problem?
Gradle script tries to push docker image to repository which doesn't exist. Do you intend to do that? If not - then just set push = false.
If you want to push the image to some repository, then you have to setup it. You have to check plugin specific settings of pushing image to repository you want.
More info:
Creating new repository on Docker Hub, Setting up own registry
I googled for ages now and I give up, the buzz word Groovy + Jenkins is bringing up so many false flags...
I have a Groovy project I developed in IntelliJ, it contains also a JUnit.groovy with unit tests. Now this is a script for SoapUI, no need for Maven, Ant nor Grails, but I would like to be able to compile those files on Jenkins and run the unit tests after. Is it possible to build and test those files on Jenkins? So far all solutions seem to be me manually running groovyc (commited with my repository) and then running JUnit on the JUnit.class.
So before I start to dig deeper and write a Maven, Grails or Ant file, is there another way that does not involve me pushing the GroovySDK on my git? Or is there may be a simple build script, not involving 20 libraries and steps that would build the groovy sources and run the JUnit tests :) ?
I'm new to Jenkins obviously ;), thanks for your input.
Update:
So for all as newbie as me, what was required? First I changed my local source code to a gradle project (remember to activate AutoImport in IntelliJ) and also activate the creation of the JUnit xml and since I do not use Maven and the system is "offline" we have the libs in git anyway so my build.gradle is:
version '2.5-SNAPSHOT'
apply plugin: 'groovy'
dependencies {
compile fileTree(dir: '../Library', include: ['*.jar'])
}
test {
reports {
junitXml.enabled = true
html.enabled = true
}
}
sourceCompatibility = 1.8
set up gradle wrapper for the project via gradle wrapper for the gradlew.bat
then I added a post-commit in my git-/.hooks/ so my Jenkins is triggered upon commit via curl http://jenkins:8080/git/notifyCommit?url=https://git.git&branches=dev
finally set up a pipeline on jenkins:
#!groovy
node {
stage('Checkout') {
git branch: 'dev', credentialsId: 'youwish', url: 'https://git.git'
}
stage('Build') {
dir('./Modules') {
gradle('clean')
gradle('compileTestGroovy')
}
}
stage('UnitTest') {
dir('./Modules') {
gradle('test')
junit '/build/test-results/**/TEST-*.xml'
}
}
stage('IntegrationTest') {
stage('CodeTableDownload') {
dir('./SoapUi') {
bat 'AutoRun.bat'
junit '/results/**/*-JUNIT.xml'
}
}
}
}
def gradle(command) {
bat "./gradlew.bat $command"
}
There's a Groovy plugin for Jenkins that will let you execute Groovy scripts on Jenkins.
But, why not let something like Gradle do the build and run the test for you? A minimal Gradle build file for Groovy that will do both is:
apply plugin: 'groovy'
repositories {
jcenter()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
testCompile 'junit:junit:4.12'
}
You don't have to commit the GDK, just declare a dependency.
I'm working with jenkins and try to call a remote machine that has a java project with gradle config to run tests build.gradle
version '1.0-SNAPSHOT'
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
sourceCompatibility = 1.8
version = '1.0'
sourceSets {
jars
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile fileTree(dir: "src/jars/resources", include: ['*.jar'])
compile group: 'org.testng', name: 'testng', version: '6.9.10'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.+'
compile group: 'io.appium', name: 'java-client', version: '4.1.2'
compile 'com.testdroid:testdroid-api:2.9'
compile group: 'org.json', name: 'json', version: '20141113'
}
compileJava {
options.encoding = "UTF-8"
}
test {
useTestNG {
suites 'src/main/resources/testng.xml'
}
}
but I want to run a custom testng2.xml! so I define in jenkins a parameter test and define their testng2.xml, but and it will show in the console
/Users/jenkins/Home/workspace/AutomationGradle/gradlew -Dtest=testng2.xml clean test -i
but I can't understand how to make gradle to get the dynamic parameter
Use it like that, with a default value set to testng.xml
ext.testFile = System.getProperty('Test_Plan') ?: 'testng.xml'
test {
useTestNG {
suites "src/main/resources/$testFile"
}
}
And run your command line with the test parameter
/Users/jenkins/Home/workspace/AutomationGradle/gradlew -DTest_Plan=testng2.xml clean test -i
Thank you, I'v worked a lot of time on this, so I'm writing what worked for me:
In the Jeninks I added a parameter:
TESTNGFILE
In the groovy file:
-DTestNG=$TESTNGFILE
build.gradle:
ext.testFile = System.getProperty('TestNG') ?: 'regression.xml'
test {
useTestNG {
options {
systemProperties(System.getProperties())
}
systemProperties = [
TESTNGFILE: System.getProperty('TESTNGFILE')
]
suites "/src/test/resources/testng/${testFile}"
testLogging.showStandardStreams = true
useDefaultListeners = true
}
}
Building a Jenkins Plugin that creates a SimpleBuildStep.
It works with maven hpi:run but I need to switch it to gradle
My problem is that when I run gradle server I can see my custom plugin is installed but it is not in the build step.
I thought it was my versioning and I changed it several times. I'm wondering if my configuration is wrong.
I have a work directory that shows up and my plugin is shown in work/plugins/ with a .hpi and a .hpl file but it still doesn't work. It only works in maven it also doesn't show when I do a docker instance of jenkins (which is always at jenkins version 2)
I'm still assuming it is my build.gradle
plugins {
id "org.jenkins-ci.jpi" version "0.16.0"
}
jenkinsPlugin {
coreVersion = "2.0" // Version of Jenkins core this plugin depends on.
displayName = "Test Jenkins Plugin" // Human-readable name of plugin.
url = "http://wiki.jenkins-ci.org/display/JENKINS/SomePluginPage" // URL for plugin on Jenkins wiki or elsewhere.
shortName = "jetson" // Plugin ID, defaults to the project name without trailing '-plugin'
}
group 'test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jenkins-ci.main', name: 'ui-samples-plugin', version: '1.424.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
EDIT: Have it working. I can actually use my plugin in my instance now.
Changes:
After examining the hpl file and reading it. I realized that my Jenkins plugin wasn't even registering my classes. I realized cause my build.gradle was in a folder in the root project. So obviously I moved build.gradle into the root.
From there I noticed it actually built those classes. Still couldn't get my plugin to actually show up as a build step even though it showed up under installed (same old problem). I took another build.gradle from a different plugin and edited for my own use. It works, however I have no idea why.
*I also had to add a missing dependency I was having, now that it was actually building my project.
new build.gradle:
buildscript {
repositories {
// The plugin is currently only available via the Jenkins
// Maven repository, but has dependencies in Maven Central.
mavenCentral()
maven {
url 'http://repo.jenkins-ci.org/releases/'
}
}
dependencies {
classpath 'org.jenkins-ci.tools:gradle-jpi-plugin:0.14.1'
}
}
apply plugin: 'java'
apply plugin: 'org.jenkins-ci.jpi'
repositories {
mavenCentral()
maven {
url "http://repo.jenkins-ci.org/releases/"
}
}
group = 'workday'
version = '0.1.0-SNAPSHOT'
description = 'Test AS A Service Plugin'
jenkinsPlugin {
// version of Jenkins core this plugin depends on, must be 1.420 or later
coreVersion = '1.654'
// ID of the plugin, defaults to the project name without trailing '-plugin'
shortName = 'jetson'
// human-readable name of plugin
displayName = 'Jetson Test Plugin'
// use the plugin class loader before the core class loader, defaults to false
pluginFirstClassLoader = true
// optional list of package prefixes that your plugin doesn't want to see from core
maskClasses = 'groovy.grape org.apache.commons.codec'
// optional version number from which this plugin release is configuration-compatible
compatibleSinceVersion = '1.1.0'
// enable injection of additional tests for checking the syntax of Jelly and other things
disabledTestInjection = false
// the output directory for the localizer task relative to the project root, defaults to the value shown
localizerOutputDir = "${project.buildDir}/generated-src/localizer"
// plugin file extension, either 'jpi' or 'hpi', defaults to 'hpi'
fileExtension = 'hpi'
}
dependencies {
compile group: 'org.jenkins-ci.main', name: 'ui-samples-plugin', version: '1.424.2'
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.14'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
I suspect it actually has to do with the new buildscripts blocks for some reason
You probably need to set your group to
group = 'org.jenkins-ci.plugins'
and you can delete the
apply plugin 'java'
as this is done internally (I think)
I'm not sure you need to include the ui-samples-plugin either but if you do it needs to be something like
dependencies {
jenkinsPlugins( group: 'org.jenkins-ci.main',
name: 'ui-samples-plugin',
version: '1.424.2',
ext: 'jar')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
(untested)
Try its wiki page for more info
How can I call the Ant target 'jar' from the Gradle build file? I've tried a number of things to no avail. Renaming the 'jar' target in the Ant build file is not an option. I'm in the process of converting an Ant based build system to Gradle and the first required step is to call all the Ant targets from Gradle.
The 'jar' task is a default Gradle task so I'm overwriting/overriding it but I need to call my similarly named Ant target.
Gradle build.gradle file:
// Prevents error "Cannot add task {taskname} as a task with that name already exists"
ant.project.addTarget('clean', new org.apache.tools.ant.Target())
ant.project.addTarget('jar', new org.apache.tools.ant.Target())
ant.project.addTarget('test', new org.apache.tools.ant.Target())
ant.project.addTarget('javadoc', new org.apache.tools.ant.Target())
ant.importBuild 'build.xml'
task jar(overwrite: true) {
println 'jar'
}
task clean(overwrite: true) {
println 'clean'
}
task test(overwrite: true) {
println 'test'
}
and my Ant build.xml:
<target name='jar' description='jar'>
<echo>Called jar task in ant build</echo>
</target>
Running Gradle v1.2
------------------------------------------------------------
Gradle 1.2
------------------------------------------------------------
Gradle build time: Wednesday, September 12, 2012 10:46:02 AM UTC
Groovy: 1.8.6
Ant: Apache Ant(TM) version 1.8.4 compiled on May 22 2012
Ivy: 2.2.0
JVM: 1.6.0_25 (Sun Microsystems Inc. 20.0-b11)
OS: Linux 2.6.37.6 amd64
Gradle only adds a jar task when you apply the Java plugin, which you shouldn't do for the project into which you import the Ant build. I don't think you should ever call ant.project.addTarget from a Gradle build script.