org.openscm.kundo.plugins.WarDelegate.groovy Maven / Gradle / Ivy
package org.openscm.kundo.plugins
/*
* 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.context.BuildContext
import java.util.jar.JarOutputStream
/**
* War Delegate
* @author Steve king
* @version 1.0.0
*
* Description: A Delegate for the creation of project WAR files
*
* The war delegate consumes data set by the kundo-project-plugin and uses each resource and src set.
* Using project artifact info and properties from either the default plugin.properties or the project's
* build.properties a war file will be created.
*/
/*
* @ant-target doWar
*/
class WarDelegate extends AbstractPluginTargetDelegate {
private static final WAR_SCOPES = ["runtime"]
/*
* The location of the application's WEB-INF directory.
* @ant-property-name war.web.folder
*/
private String webFolder
/*
* File name of a manifest file to include in the created war. Leave blank to create a default manifest in the jar file.
* @ant-property-name war.manifest
*/
private String manifest
/*
* The location the war file will be created at.
* @ant-property-name war.destination.folder
*/
private String destinationFolder
//Path Separator
private String ps = File.pathSeparator
/**
* Constructor sets ant and buildContext instances in super class
* @param ant AntBuilder instance
* @param buildContext BuildContext instance
*/
WarDelegate( AntBuilder ant, BuildContext buildContext ){
super( ant, buildContext)
}
/**
* Implementation of abstract method from AbstractPluginTargetDelegate
* to hold assertions that all global and local properties are available to the plugin.
* This method is automatically called prior to execution.
*/
void doCheck(){
assert webFolder != null
assert manifest != null
assert destinationFolder != null
assert buildContext.get( "project.properties" ) != null
}
/**
* Setter method for member variable webFolder.
* @param webFolder String representation of the application's WEB-INF directory
*/
void setWebFolder( String webFolder ){
this.webFolder = webFolder
if( !( webFolder.endsWith( '/' ) || ( webFolder.endsWith( '\\' ) ) ) )
this.webFolder = this.webFolder + '/'
}
/**
* Setter method for member variable manifest.
* @param manifest String representation of optional manifest location
*/
void setManifest( String manifest ){
this.manifest = manifest
}
/**
* Setter method for member variable destinationFolder.
* @param destinationFolder String representation of war output directory
*/
void setDestinationFolder( String destinationFolder ){
this.destinationFolder = destinationFolder
if( !( destinationFolder.endsWith( '/' ) || ( destinationFolder.endsWith( '\\' ) ) ) )
this.destinationFolder = this.destinationFolder + '/'
}
/**
* Concrete implementation of the AbstractDelegate.execute() method
*/
void doExecute(){
// Retrieve the indexed project properties from the global context
Map properties = buildContext.get( "project.properties" )
List srcProjectProperties = properties.get( "src" )
List resourceProjectProperties = properties.get( "resources" )
String artifactName = ant.project.getProperty( "global.artifactid" )
String warDest = destinationFolder + artifactName + '.war'
String webxmlLocation = webFolder + 'WEB-INF/web.xml'
if (log.isDebugEnabled() ) {
log.debug( "Creating War: $artifactName" )
log.debug( "webFolder: $webFolder")
log.debug( "webxmlLocation: $webxmlLocation")
log.debug( "artifactName: $artifactName")
log.debug( "warDest: $warDest")
}
def warOptions = [destfile:warDest, webxml:webxmlLocation ]
// check if a manifest file has been included, if not we want a default manifest building for us by ant
if ( !manifest.equals( "" ) )
warOptions.put( "manifest", manifest )
//delete previously created WAR
ant.delete( file:warDest )
// create WAR file usingteh ant.war task
ant.war( warOptions ){
ant.fileset( dir:webFolder ) //add web.xml
// add classes to war
srcProjectProperties.each{projectProperty ->
if ( log.isDebugEnabled() )
log.debug( "class: " + projectProperty )
ant.classes( dir:projectProperty.output )
}
//add resources to war
resourceProjectProperties.each{ projectProperty ->
if (log.isDebugEnabled() )
log.debug( "resources: " + projectProperty)
ant.classes( dir:projectProperty.output )
}
//add dependencies to WAR
WAR_SCOPES.each{ scope ->
if(buildContext.isSet("${artifactName}.${scope}.classpath" )){
String dependencies = buildContext.get( "${artifactName}.${scope}.classpath" )
dependencies.split( "${ps}" ).each{dependency ->
if ( log.isDebugEnabled() )
log.debug( "$scope dependency: " + dependency )
ant.lib( file:dependency )
}
}
}
}
log.debug( "ant war task done" )
ant.property( name : "global.artifact.location", value : warDest )
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy