Use another Jenkins plugin from my plugin - jenkins

I'm writing a Jenkins plugin that has some complex logic for sending slack notifications - more than just "build failed", or "build succeeded". Within my plugin, I'd like to use the slacksend plugin to send slack messages. How would I go about doing that?

In the pom.xml of your plugin, declare a dependency on the plugin you want to use. In youd code, use classes from the dependency. You'll find groupId, artifactId and version here:
https://github.com/jenkinsci/slack-plugin/blob/master/pom.xml
You'll also want to add this section, as Jenkins plugins are not available in Maven Central.
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

Related

Why does maven 3.2 download same artifact multiple times from all repositories defined in pom

I have defined 2 repositories in pom.xml like below. One is public and other is thirdparty.
<repositories>
<repository>
<id>public</id>
<name>Nexus - Public Repositories</name>
<layout>default</layout>
<url>http://mavenrepo.aaa.net/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>thirdparty</id>
<name>Nexus - Third Party</name>
<layout>default</layout>
<url>http://mavenrepo.aaa.net/nexus/content/repositories/thirdparty</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
Say there is a dependency called grpId:artId:1.1.0 which I have defined in my pom. This dependency is present in both the repositories defined above- public and thirdparty
When I run mvn install I see different behaviours in the way maven downloads the grpId:artId:1.1.0 artifact based on the version of maven i am using.
Behaviours :-
1. Using Maven 3.1 and previous versions
a. Maven looks up the dependency grpId:artId:1.1.0 in 'public' repo
b. Maven finds the dependency and downloads it.
c. Maven does not look up the dependency grpId:artId:1.1.0 in
'thirdparty' repo as it is already downloaded from previous repository.
2. Using Maven 3.2
a. Maven looks up the dependency grpId:artId:1.1.0 in 'public' repo
b. Maven finds the dependency in 'public' repo and downloads it.
c. Maven again looks up the dependency grpId:artId:1.1.0 in
'thirdparty' repo even though it is already downloaded from 'public' repository.
d. Maven finds the dependency in 'thirdparty' repo and downloads it
and overwrites the dependency downloaded from 'public' repo previously
I wonder why maven is behaving in an absurd manner for maven-3.2. It should stop looking further for the dependency which is already resolved and downloaded from one repository .
Is there a way to achieve it using maven 3.2 ?
This was a bug identified and fixed by maven in version 3.2.5
https://issues.apache.org/jira/browse/MNG-5723

Jenkins conditional steps for maven project

I use Jenkins for continuous integration.
How can I run conditional steps with Maven projects ? For free style projects I am able to set up conditional steps but this seems not available with Maven projects.
Additionally I also want to publish findbugs/pmd reports conditionally in maven projects. Again this looks possible using flexible publish plugin only for free style projects.
Any help would be highly appreciated.
-Thanks
You can add a profile in your POM and activate it only when a variable (Only setted by Jenkins for example) is present. A bit like this:
<profiles>
<!-- Jenkins by default defines a property BUILD_NUMBER which is used to
enable the profile. -->
<profile>
<id>coverage</id>
<activation>
<property>
<name>env.BUILD_NUMBER</name>
</property>
</activation>
<build>
</build>
</profile>
</profiles>
Alternatively, you can add some profiles add using them manually in the Jenkins command line:
mvn -P coverage

Maven3 multiple repos

I'm new to Maven.
I have a POM that does not specify a repo, and I want to download Spring's mobile stuff, and the maven directions specify a repo:
<repository>
<id>springsource-repo</id>
<name>SpringSource Repository</name>
<url>http://repo.springsource.org/release</url>
and
<repository>
<id>springsource-milestone</id>
<name>SpringSource Milestone Repository</name>
<url>http://repo.springsource.org/milestone</url>
How do I edit my POM to use these repos for the Spring mobile dependencies, but keep on using the master repo for the other dependencies?
Did you try out adding repositories tag as mentioned in maven.apache.org/guides/mini/guide-multiple-repositories.html

Maven: dynamic specification of dependencies

I have started to learn Maven and have the following question:
I would like to dynamically specify a dependency for building maven project instead of using the dependency specified in POMs - is there a way to do that?
So although I have the following dependencies specified in POM
...
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>ProjectComponent</artifactId>
<version>1.0</version>
</dependency>
...
I would like to specify in the build command that I want to use a different version.
Is there a way to specify this?
The idea is that I want to have an integration build made in Jenkins with a dependency on the latest available snapshot of the system for a particular branch. That snapshot is not released to the maven repository yet, so I would like to fetch it in Jenkins and specify a dependency for mvn build.
Thanks!
POSSIBLE SOLUTION: What I ended up with is to use ${my.lib.version} construction and specify it with -Dmy.lib.version=1.0-SNAPSHOT" when calling to mvn. Thus I can use it for Jenkins integration builds by fetching arbitrary snapshot versions of dependencies from svn and feeding their snapshot versions to the integration build pom.
Maven may use "dynamically" specified property (ex: group.ProjectComponent.version) with the help of profiles.
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>ProjectComponent</artifactId>
<version>${group.ProjectComponent.version}</version>
</dependency>
So if you create some profiles you may switch between them (see maven references)
Example:
<profile>
<id>stable-builds</id>
<properties>
<group.ProjectComponent.version>1.0</group.ProjectComponent.version>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>beta-builds</id>
<properties>
<group.ProjectComponent.version>2.0.Beta1</group.ProjectComponent.version>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
What I ended up with is to use ${my.lib.version} construction and specify it with -Dmy.lib.version=1.0-SNAPSHOT" when calling to mvn. Thus I can use it for Jenkins integration builds by fetching arbitrary snapshot versions of dependencies from svn and feeding their snapshot versions to the integration build pom.
Just came across this as I was looking for something similar. In my case same application code is being reused on different stacks which means using different "drivers" for accessing data. Although drivers implement same interface they do come from different artifacts.
No you can't change the dependencies dynamically. Furthermore it does not make sense, cause you would like to have a reproducible build.

Using Maven API download a JAR

In a simple java program, How can I download a JAR from a Maven repository ?
The repository can be local as well as remote ? I am using Maven 3.
As noted by #amit your use of Maven-3 is not relevant. Your application is interested in accessing a JAR at runtime. It just so happens that this JAR is available at a Maven repository. Maven is a build-time tool. It cannot help you at runtime.
So if we have interpreted your question correctly, the issue is one of formulating the URL and making an HTTP request. Since you say the JAR is hosted by a Maven repository you know the format of the URL:
http://repository.url/group_id_segments_separated_with_slashes/artifact_id/version/artifact_id-version.jar
You can take advantage of this in your program if you need to access more than one JAR in this fashion.
define the necessary mapping for dependency tags in POM.xml and than provide repository information inside repository tags... for example..
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.11</version>
</dependency>
and
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/snapshots/</url>
<layout>default</layout>
</repository>
see more about this here

Resources