Automate Jenkins job creation with DSL

28 02 2016

Jenkins job creation in Web UI is simple but still – manual creation takes time. Depending on your coding and testing practices you may find it acceptable or burdensome and costly.

Jenkins DSL allows you to define Jenkins job in groovy script file, put it on VCS and allow Jenkins to create and execute jobs based on it. The another advantage is that you get your jobs versioned along with codebase.

So… is that hard? Having Jenkins configured to build your code it requires few steps to build jobs from DSL:

1. Install Jenkins Job DSL plugin

Simple thing to begin: go to your Jenkins’ Management page, find the Job DSL plugin and install it (https://wiki.jenkins-ci.org/display/JENKINS/Job+DSL+Plugin).

2.Create the Seed Job

That is the job that will create other jobs. Do it like any other job creation: Select the ‘New Item‘, give it a name (in my case ‘dsl-demo’) and select type of ‘Build a free-style software project‘ (or ‘Freestyle project’).

Remember to configure the Code Repository url and credentials where this job will do the checkout and find the DSL script files.

3. Configure DSL scripts files location

Then in your job configuration add build step named: ‘Process Job DSLs’ (provided by Job DSL plugin). I am going to use the script files located in the filesystem (Jenkins’ workspace), versioned in my git repo. All of them will be located under the Jenkins/jobs/ directory in my VCS with the .groovy extension. So the path in my config is:

Jenkins/jobs/*.groovy

Where I have one file as for now, named:

Jenkins/jobs/demoJob.groovy

Remember to follow naming conventions for files: script names may only contain letters, digits and underscores, but may not start with a digit

4. DSL Script file content

First of all to simply make Jenkins create the job it is sufficient to have such content in DSL script:

job('simpliest-job-ever') {
    steps {
        shell('echo hello from your automated job!')
    }
}

Here I am creating the job with a name ‘simpliest-job-ever’ with one step inside. The step is using shell plugin and executing the echo command.

5. Run Seed Job

Now is the time to push the DSL file to the git server and trigger the Seed Job execution. The effect found in Jenkins console after running my Seed job was:

Processing DSL script demoJob.groovy
Added items:
    GeneratedJob{name='simpliest-job-ever'}

So my job was created from DSL file.

6. Run newly created Job

Now, coming back to the Jenkins’ job list the new job actually is there. I did not set the triggers on it yet so I have to fire it manually.

Here is the result of executing my newly generated job, printed in Jenkins console:

Started by user LooksOK!
Building in workspace /var/lib/jenkins/jobs/simpliest-job-ever/workspace
+ echo hello from your automated 'job!'
hello from your automated job!
Finished: SUCCESS

Lovely, isn’t it? :>

Enhance your DSL script

Such a basic configuration and script file prooves that the whole thing works. Now is the time to enhance the scripts and make them creating the fully useful jobs. The plugins supported in DSL and their full documentation is here: https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-DSL-Commands.

A list of most useful plugins is:

    • SCM to connect your Job with code repository
scm {
  git('git@github.com:yacekmm/looksok.git')
}
    • Triggers to setup the schedule on your job execution
triggers {
  scm('*/15 * * * *')
}
    • maven to execute maven tasks
maven('-e clean test')
    • shell to execute shell commands
shell('echo hello from your automated job!')
  • and more…

Thanks for reading, give it a try!

 





Maven dependency visualization in JEE7

19 07 2014

Another short, concise and informative Adam Biens’ scrrencast, this time about Visual EE Maven plugin that visualizes JEE 7 app dependencies, events, CDI, EJBs and JPA relations.

Watch the video here:





Eclipse: Aspectj Maven plugin execution not covered by lifecycle

7 06 2014

aspectj-logoMaven and eclipse integration is not straightforward. There is one m2e eclipse plugin giving support for Maven itself and a lot of other Eclipse plugins to support Maven plugins used in pom.xml.

One of these plugins is the org.codehaus.mojo:aspectj-maven-plugin – the aspectJ Maven plugin. So when you use it in pom.xml and import into eclipse, you will get error:

Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:aspectj-maven-plugin:1.2:compile (execution: default, phase: process-sources), Maven Project Build Lifecycle Mapping Problem

To fix it

Install AspectJ Maven Eclipse plugin from Eclipse update site (Via eclipse Help -> Install new software…):

http://dist.springsource.org/release/AJDT/configurator/

Prerequisities

To install above maven plugin Eclipse needs to have AspectJ Development Tools Eclipse Plugin and AspectJ compiler installed. If you do not have it, you need to install as well. This is where you can find it (install via eclipse Help -> Install new software…):

http://download.eclipse.org/tools/ajdt/43/update/

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Maven and apklib: Duplicated file error

31 05 2014

The maven build error occurred when I had two Android projects: the one is apklib and the second one that is the actual application (the apk file) that uses apklib.

The error is saying that apk project contains duplicated R.java and BuildConfig.java files under target directory. The first file comes from apk project, and the second one from apklib.

There are two solutions for this problem:

Solution 1: Update android-maven-plugin

Update android-maven-plugin to the newest version (currently it is 3.8.2 or 3.9.0 release candidate to be checked here) where this bug is fixed.

The problem is that for example the 3.8.2 release is supporting only maven in version 3.1.1. no lower nor higher version will work. This would require downgrading maven on few machines so I decided to use another solution.

Solution 2: Remove conflicting files during apklib build with maven ant plugin

I’ve found solution on the Internet to remove duplicated files from the apklib target with ant plugin during build. The maven-antrun-plugin can do it. I added these lines to apklib pom.xml:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<executions>
		<execution>
			<phase>process-classes</phase>
			<goals>
				<goal>run</goal>
			</goals>
			<configuration>
				<tasks>
					<delete dir="${project.basedir}/gen" />
				</tasks>
			</configuration>
		</execution>
	</executions>
</plugin>

Then rebuild the apklib project and apk after that. This works!

Eclipse problems with ant plugin

Ant plugin is not recognized by eclipse maven m2e connector and eclipse displays errors. the project still can be built with either maven or eclipse build, but the error is visible on the pom.xml.

Then with plugin management in apklib you can force eclipse to ignore the ant plugin command. It does not affect maven build, just the eclipse compilation.

You can do it by eclipse quick fix or by adding manually plugin management directive to apklib’s pom.xml:

<pluginManagement>
<plugins>
	<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
	<plugin>
		<groupId>org.eclipse.m2e</groupId>
		<artifactId>lifecycle-mapping</artifactId>
		<version>1.0.0</version>
		<configuration>
			<lifecycleMappingMetadata>
				<pluginExecutions>
				<pluginExecution>
					<pluginExecutionFilter>
						<groupId>
							org.apache.maven.plugins
						</groupId>
						<artifactId>
							maven-antrun-plugin
						</artifactId>
						<versionRange>
							[1.3,)
						</versionRange>
						<goals>
							<goal>run</goal>
						</goals>
					</pluginExecutionFilter>
					<action>
						<ignore></ignore>
					</action>
				</pluginExecution>
				</pluginExecutions>
			</lifecycleMappingMetadata>
		</configuration>
	</plugin>
</plugins>
</pluginManagement>




Maven & Android: complete pom.xml files contents

17 11 2012

The complete pom.xml file I use for Android projects is as follows.

1. Main Android application’s pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>pl.looksok</groupId>
	<artifactId>ColCalc</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>apk</packaging>

	<build>
		<sourceDirectory>${project.basedir}/src</sourceDirectory>
		<plugins>
			<plugin>
				<groupId>com.jayway.maven.plugins.android.generation2</groupId>
				<artifactId>android-maven-plugin</artifactId>
				<version>3.3.2</version>
				<configuration>
					<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
					<assetsDirectory>${project.basedir}/assets</assetsDirectory>
					<resourceDirectory>${project.basedir}/res</resourceDirectory>
					<genDirectory>${project.basedir}/gen</genDirectory>
					<deleteConflictingFiles>true</deleteConflictingFiles>
					<undeployBeforeDeploy>true</undeployBeforeDeploy>
					<sdk>
						<platform>7</platform>
					</sdk>
				</configuration>
				<extensions>true</extensions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- java project dependency -->
		<dependency>
			<groupId>pl.looksok.logic</groupId>
			<artifactId>ColCalcLogic</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<!-- android apklib project dependency -->
		<dependency>
			<groupId>pl.looksok.currencyedittext</groupId>
			<artifactId>CurrencyEditText</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<type>apklib</type>
		</dependency>
	</dependencies>
</project>

Please remember to set target Android SDK and java version in file above. In my case Java version is 1.6, and SDK is 7. Keep the same config in AndroidManifest.xml.

2. apklib – Android library project pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>pl.looksok.currencyedittext</groupId>
  <artifactId>CurrencyEditText</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- set up packaging -->
  <packaging>apklib</packaging>
  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
            <plugins>
                <plugin>
			<groupId>com.jayway.maven.plugins.android.generation2</groupId>
			<artifactId>android-maven-plugin</artifactId>
			<version>3.1.1</version>
			<configuration>
				<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
				<assetsDirectory>${project.basedir}/assets</assetsDirectory>
				<resourceDirectory>${project.basedir}/res</resourceDirectory>
				<genDirectory>${project.basedir}/gen</genDirectory>
				<deleteConflictingFiles>true</deleteConflictingFiles>
				<undeployBeforeDeploy>true</undeployBeforeDeploy>
				<sdk>
					<platform>7</platform>
				</sdk>
			</configuration>
			<extensions>true</extensions>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.5.1</version>
			<configuration>
				<source>1.6</source>
				<target>1.6</target>
			</configuration>
		</plugin>

        </plugins>

        <pluginManagement>
		<plugins>
		<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
		<plugin>
			<groupId>org.eclipse.m2e</groupId>
			<artifactId>lifecycle-mapping</artifactId>
			<version>1.0.0</version>
			<configuration>
				<lifecycleMappingMetadata>
				<pluginExecutions>
				<pluginExecution>
				<pluginExecutionFilter>
					<groupId>com.jayway.maven.plugins.android.generation2</groupId>
		  			<artifactId>android-maven-plugin</artifactId>
		  			<versionRange>[3.3.0,)</versionRange>
		  			<goals>
		  				<goal>proguard</goal>
		  			</goals>
		  		</pluginExecutionFilter>
		  		</pluginExecution>
		  		</pluginExecutions>
		  		</lifecycleMappingMetadata>
		  	</configuration>
		  </plugin>
		</plugins>
	</pluginManagement>
  </build>
</project>

The most important change to notice is the packaging tag at the beginning – set it to apklib.

Note that eclipse does not read maven apklib dependencies from pom.xml automatically. If you want your android project to use apklib project in eclipse, you have to manually add library in your main project’s settings:

Please note also, that your apklib project has to be marked as Android Library in its properties (check it in the same project properties window as shown above). Maven plugin however should automatically mark this project as library, based on its pom.xml file.

3. jar – standard java library’s pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>pl.looksok.logic</groupId>
  <artifactId>ColCalcLogic</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Just as a remainder – this is standard pom.xml for jar library

Summary

These files are operational, after performing all maven, m2eclipse and maven for android plugin configurations

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Use Maven with Android project

12 05 2012

In order to mavenize (existing or not) Android project in Eclipse few steps are required:

1. Install m2eclipse plugin

All instructions how to do it, You will find here.

2. Install ‘Android Configurator for M2E

in Eclipse go to menu:

Help -> Eclipse Marketplace

search for ‘Android Configurator for M2E’ and install it:

Here is plugin’s website. Any additional info will be provided there. Moreover here is very detailed description about Android development with Maven.

3. Install Maven 3.0.3

Android Configurator for m2e requires Maven in version 3.0.3 or higher, and m2eclipse comes with lower version. There is need to update it. Use download source site for Maven 3.0.4 and unzip it. Then in eclipse go to:

Window -> Preferences -> Maven -> Installations

and set path to downloaded files (click ‘Add‘):

4. Create pom.xml

Here is sample pom.xml from plugin’s website. The most important differences between this, and standard pom.xml are marked with green.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.simpligility.android</groupId>
    <artifactId>helloflashlight</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>HelloFlashlight</name>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>1.6_r2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>src</sourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>3.2.0</version>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <configuration>
                    <sdk>
                        <!-- platform or api level (api level 4 = platform 1.6)-->
                        <platform>4</platform>
                    </sdk>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

If you need some more info on basic configuration of pom.xml for m2eclipse, look  here.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Basic Maven (m2eclipse / pom.xml) configuration

5 05 2012

Introducing Maven to Eclipse projects from scratch was described in my previous post. Now the basic usage and configuration will be described.

1a. Adding external jars (Maven dependencies)
To add some external jar file or reference to another workspace project you will need to:

Right Click on project -> Maven -> Add dependency

You can add it by filling in groupId, version etc. manually or search Maven repository by keyowrds. For example searching for JSON lib looks like in the picture below. After confirm, you will see changes reflected in pom.xml file.


1b. Another workspace project dependency
It is also possible to refer to another project, however it also has to be the Maven one (has its own pom.xml). Some instructions can be found here. Sample snippet of dependency in pom.xml is:

<dependency>
  <groupId>com.maventest</groupId>
  <artifactId>mytest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>compile</scope>
</dependency>

2. Setting the src folder
The issue after adding Maven nature to project in my case was Eclipse missing the source folder in project. src was present in workspace, however it was not recognized as source folder. The solution is to add following lines in pom.xml file (if more info is needed look at this source):

 <build>
   <sourceDirectory>${basedir}/src</sourceDirectory>
 </build>

Note: Setting source folder by eclipse standard way (Build path -> Configure Build Path) will have no effect, because it will be reverted after Updating Maven Configuration.

3. Set JRE for project
If you want to use specific Java version in Maven project, you can use Maven plugin: compiler-plugin, as described here. It’s easy and requires only adding this entry to pom.xml file:

<plugins>
  ..
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>
</plugins>

4. Problem with selecting targets for Maven builds
If You have problem with selecting targets (empty goals list), see solution here and corresponding bug description here.

It did not help me :D However solution makes sense and worth to try it anyway :)

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!








%d bloggers like this: