
org.kuali.maven.plugins.spring.LoadMojo Maven / Gradle / Ivy
/**
* 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.springframework.util.Assert;
/**
*
* 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
. Prior to the Spring context being loaded, it is injected with a
* java.util.Properties
object containing the full set of Maven properties. The java.util.Properties
object is
* registered in the context as a bean under the name mavenProperties
.
*
*
* One common use of the injected Maven properties in a Spring context is for replacing property placeholders.
*
*
* For example:
*
*
*
* <beans>
* <context:property-placeholder properties-ref="mavenProperties" />
* <bean id="artifactId" class="java.lang.String">
* <constructor-arg value="${project.artifactId}" />
* </bean>
* </beans>
*
*
* @goal load
*/
public class LoadMojo extends AbstractMojo {
/**
* 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;
/**
* If true, Maven properties are injected into the Spring context as a java.util.Properties
object
*
* @parameter expression="${spring.injectProperties}" default-value="true"
*/
private boolean injectProperties;
/**
* If true, the MavenProject
object is injected into the Spring context
*
* @parameter expression="${spring.injectProject}" default-value="false"
*/
private boolean injectProject;
/**
* 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="mavenProperties"
*/
private String propertiesBeanName;
/**
* The name to use when registering the MavenProject
object as a bean in the Spring context.
*
* @parameter expression="${spring.projectBeanName}" default-value="project"
*/
private String projectBeanName;
/**
* 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(location, locations);
// Log what we are up to
logConfiguration();
// Invoke the service to load the context
SpringService service = getService(serviceClassname);
service.load(locations, getBeanNames(), getBeans());
}
protected List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy