All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kuali.maven.plugins.spring.LoadMojo Maven / Gradle / Ivy

Go to download

This plugin provides integration between Spring and Maven. Plugin goals support loading a Spring context XML file as part of the Maven build lifecycle. The XML file can be on the local file system or be accessible via any URL Spring's resource loading mechanism can understand. Spring's "classpath:context.xml" style notation is supported. Annotated Java classes can also be used to load a Spring context. Maven properties are injected into the Spring context (both XML and annotation style) as a java.util.Properties bean named "mavenProperties". Maven properties are also registered as a top level PropertySource so that Spring's placeholder resolution framework automatically considers them. See Project Reports -> Plugin Documentation for details on plugin goals. By default, the profile "maven" is set as an active Spring profile along with any other active Maven profiles.

There is a newer version: 3.1.0
Show newest version
/**
 * Copyright 2011-2012 The Kuali Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 *
 * 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.
 */
package org.kuali.maven.plugins.spring;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.kuali.common.util.PropertyUtils;
import org.kuali.common.util.service.SpringService;
import org.kuali.common.util.spring.SpringContext;

/**
 * 

* This mojo provides the ability to load a Spring context XML file. It uses a lightweight integration technique between Spring and Maven * centered around java.util.Properties. Given the location of a Spring XML context, the mojo loads and injects it with a * java.util.Properties object containing an augmented set of Maven properties. The java.util.Properties object is * registered in the context as a bean under propertiesBeanName which defaults to maven.spring.properties. One * typical use of the injected Maven properties in a Spring context is for replacing property placeholders. *

*

* For example: *

* *
 *  <beans>
 *
 *   <context:property-placeholder properties-ref="maven.spring.properties" />
 *
 *   <bean id="artifactId" class="java.lang.String">
 *    <constructor-arg value="${project.artifactId}" />
 *   </bean>
 *
 *  </beans>
 * 
* * @goal load */ public class LoadMojo extends AbstractMojo implements SpringContext { /** * Maven project * * @parameter default-value="${project}" * @required * @readonly */ private MavenProject project; /** * Location of a Spring context XML file. This can be a file on the local file system, or any URL Spring's Resource loading framework * understands eg {@code classpath:my-context.xml} * * @parameter expression="${spring.location}" default-value="classpath:${project.artifactId}-context.xml" * @required */ private String location; /** * List of additional Spring context XML files to load (if any). * * @parameter */ private List locations; /** * Additional properties to supply to the Spring context. * * @parameter */ private Properties properties; /** * The name to use when registering the java.util.Properties object containing Maven properties as a bean in the Spring * context. * * @parameter expression="${spring.propertiesBeanName}" default-value="maven.spring.properties" * @required */ private String propertiesBeanName; /** * The implementation of {@code org.kuali.common.util.service.SpringService} to use * * @parameter expression="${spring.serviceClassname}" default-value="org.kuali.common.util.service.DefaultSpringService" * @required */ private String serviceClassname; @Override public void execute() throws MojoExecutionException { // The ordering here is significant. // Properties supplied directly to the mojo override properties from project.getProperties() // But, internal Maven properties need to always win. // ${project.artifactId} needs to always faithfully represent the correct artifactId this.properties = PropertyUtils.combine(project.getProperties(), properties, MavenUtils.getInternalProperties(project)); // Combine the list with the single value this.locations = combine(locations, location); // Invoke the service to load the context invokeService(serviceClassname); } protected void invokeService(String serviceClassname) { try { Class serviceClass = Class.forName(serviceClassname); SpringService service = (SpringService) serviceClass.newInstance(); service.load(this); } catch (ClassNotFoundException e) { throw new IllegalStateException("Unexpected error", e); } catch (IllegalAccessException e) { throw new IllegalStateException("Unexpected error", e); } catch (InstantiationException e) { throw new IllegalStateException("Unexpected error", e); } } protected List combine(List locations, String location) { if (locations == null) { return Collections.singletonList(location); } else { // Insert location as the first element in the list List combined = new ArrayList(locations); combined.add(0, location); return combined; } } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } @Override public List getLocations() { return locations; } public void setLocations(List locations) { this.locations = locations; } @Override public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String getPropertiesBeanName() { return propertiesBeanName; } public void setPropertiesBeanName(String propertiesBeanName) { this.propertiesBeanName = propertiesBeanName; } public String getServiceClassname() { return serviceClassname; } public void setServiceClassname(String serviceClassname) { this.serviceClassname = serviceClassname; } public MavenProject getProject() { return project; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy