
org.openscm.kundo.strategy.EclipseModuleStrategy.groovy Maven / Gradle / Ivy
The newest version!
package org.openscm.kundo.strategy
/*
* Copyright (C) 2008 The Ultimate People Company Ltd ("UPCO").
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.openscm.kundo.plugins.Module
import org.openscm.kundo.common.PropertyUtils
import org.apache.tools.ant.BuildException
/**
* Eclipse Module Strategy
* @author Ben Leedham
* @version 1.0.0
*
* Description: The Eclipse Module Strategy loads a list of modules based an eclipse project set file.
*
* The Eclipse Module Strategy creates a list of modules based on the eclipse team project set file.
* The project set file is parsed by using ant4eclipse ant tasks to obtain the build order, this build order
* is then executed as separate ant builds.
*
* The eclipse project set file points to projects in your eclipse workspace, therefore Kundo needs to know
* the location of the eclipse workspace. It is necessary for this property to be externalised to allow the
* build to be run in different environments, to work with the eclipse strategy you must specify the
* ECLIPSE_WORKSPACE_LOCATION environment variable and point it to the eclipse workspace directory.
*/
/*
* @ant-target doReactor
*/
public class EclipseModuleStrategy extends ModuleStrategy {
/*
* variable to hold the location of the project workspace
*/
def workspace
/**
* Constructor passes the ant instance in a call to super
* @param ant AntBuilder instance
*/
EclipseModuleStrategy( ant ) {
super( ant )
if( !PropertyUtils.queryProjectForTask( ant, "getSourcepath" ) ){
ant.'taskdef'( resource:"net/sf/ant4eclipse/antlib.xml", classpathref:"kundo-reactor-plugin.dependency.path" )
}
}
/**
* Setter method for member variable workspace
* @param workspace String representation of the workspace location
*/
void setWorkspace( String workspace ){
this.workspace = workspace
}
/**
* Concrete implementation of the ModuleStrategy.loadModules() method
*
* Loads a list of modules based upon the eclipse projects team project set file. The project set file is obtained through the
* ECLIPSE_WORKSPACE_LOCATION environment variable, or searching a path relative to the project.
*/
void loadModules( buildContext, childBuildFile, childTarget , inheritConfiguration, profiles ) {
def basedir = ant.project.getProperty( "basedir" )
if( workspace == null ){
workspace = findWorkspace( basedir )
}
def artifactId = buildContext.get( "artifactid" )
def projectSet = buildContext.get( "eclipse.project.set" )
if( projectSet == null || projectSet == "" ){
def referencedProjects = buildContext.get( "${artifactId}.referenced.projects" )
ant.getBuildOrder( workspace:workspace, projectNames:referencedProjects, buildorderProperty:"${artifactId}.build.order" )
}else{
ant.getBuildOrder( workspace:workspace, projectSet:projectSet, buildorderProperty:"${artifactId}.build.order" )
}
def findModuleDir = { projName ->
log.debug( "locating ${projName}'s directory" )
ant.getOutputpath( workspace:workspace, projectName:projName, property:"module.${projName}.outputpath" )
def outputpath = ant.project.getProperty( "module.${projName}.outputpath" )
def projNameOutDir
if( outputpath != null && outputpath != "" ){
projNameOutDir = outputpath.substring( 0, ( outputpath.indexOf( projName ) + projName.length() ) + 1 )
}else{
throw new BuildException( "Unable to find module directory for module: ${projName}" )
}
return projNameOutDir
}
// Closure to build a module from the project module element
def addModule = { m, mDir ->
log.debug( "Creating module with params ${m}, ${mDir}" )
def Module module = new Module()
module.name = m
module.buildFile = childBuildFile
module.dir = mDir
module.target = childTarget
module.inheritConfiguration = inheritConfiguration
modules.add(module)
}
def projectModules = ant.project.getProperty( "${artifactId}.build.order" )
// Check for any top level modules and process accordingly
if( projectModules != null && projectModules != "" ){
projectModules.split( "," ).each{ m ->
addModule( m, findModuleDir( m ) )
}
}else{
throw new BuildException( "Unable to find build order from workspace: ${workspace}" )
}
}
/**
* Method to find the workspace location, currently searches for an environment variable if that doesn't exist
* it looks in the current projects parent directory for a directory named .metadata if neither can be found
* then a MissingPropertyException will be thrown.
*
* @param projectDir String representation of the current project's directory
* @return String representation of the workspace containing the current projject and it's referenced projects
* @throws MissingPropertyException if no workspace location can be identified
*/
String findWorkspace( String projectDir ){
def workspaceLoc = System.getenv().get( "ECLIPSE_WORKSPACE_LOCATION" )
if( workspaceLoc == null || workspaceLoc == "" ){
File proj = new File( projectDir )
def projParent
if( proj.exists() ){
projParent = proj.getParentFile()
}
def projParentFiles
if( projParent != null ){
projParentFiles = projParent.listFiles()
}
if( projParentFiles != null ){
projParentFiles.each{ file ->
if( file.getName().equals( ".metadata" ) ){
workspaceLoc = projParent.getAbsolutePath()
}
}
}
}
if( workspaceLoc == null || workspaceLoc == "" ){
throw new MissingPropertyException( "Unable to find the workspace for discovery of referenced projects. Please set your ECLIPSE_WORKSPACE_LOCATION environment variable to the location of the workspace containing this project and it's referenced projects." )
}
return workspaceLoc
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy