Created
March 7, 2012 11:36
-
-
Save jcsirot/1992647 to your computer and use it in GitHub Desktop.
Jenkins injector for maven project version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jenkinsci.plugins.mavenpropsinject; | |
import hudson.Extension; | |
import hudson.Launcher; | |
import hudson.maven.AbstractMavenProject; | |
import hudson.maven.MavenModule; | |
import hudson.maven.MavenModuleSet; | |
import hudson.model.AbstractBuild; | |
import hudson.model.AbstractProject; | |
import hudson.model.BuildListener; | |
import hudson.tasks.BuildWrapper; | |
import hudson.tasks.BuildWrapperDescriptor; | |
import org.kohsuke.stapler.DataBoundConstructor; | |
import java.io.IOException; | |
import java.util.Map; | |
/** | |
* This wrapper injects a the {@code MAVEN_PROJECT_VERSION} property containing the maven project | |
* version (root module version for multi-module projects). | |
* | |
* @author Jean-Christophe Sirot | |
*/ | |
public class MavenPropertiesInjectorBuildWrapper extends BuildWrapper | |
{ | |
@DataBoundConstructor | |
public MavenPropertiesInjectorBuildWrapper() | |
{ | |
} | |
@Override | |
public Environment setUp(AbstractBuild build, final Launcher launcher, BuildListener listener) | |
throws IOException, InterruptedException | |
{ | |
AbstractMavenProject project = (AbstractMavenProject)build.getProject(); | |
final String version; | |
if (project instanceof MavenModuleSet) { | |
MavenModule module = ((MavenModuleSet)project).getRootModule(); | |
version = module.getVersion(); | |
} else if (project instanceof MavenModule) { | |
version = ((MavenModule)project).getVersion(); | |
} else { | |
throw new RuntimeException("Not a maven project"); | |
} | |
launcher.getListener().getLogger().append("Injected project version: ").append(version).append("\n"); | |
return new Environment() { | |
@Override | |
public void buildEnvVars(Map<String, String> env) | |
{ | |
launcher.getListener().getLogger().append(env.toString()).append("\n"); | |
env.put("MAVEN_PROJECT_VERSION", version); | |
} | |
}; | |
} | |
@Extension | |
public static final class DescriptorImpl extends BuildWrapperDescriptor | |
{ | |
@Override | |
public boolean isApplicable(AbstractProject<?, ?> item) | |
{ | |
return item instanceof AbstractMavenProject; | |
} | |
@Override | |
public String getDisplayName() | |
{ | |
return "Maven Properties Injector"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment