View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.maven.plugins.spring;
17  
18  import java.util.Properties;
19  
20  import org.apache.maven.project.MavenProject;
21  import org.kuali.common.util.LocationUtils;
22  import org.kuali.common.util.PropertyUtils;
23  import org.kuali.common.util.property.GlobalPropertiesMode;
24  import org.kuali.common.util.service.SpringService;
25  
26  public class MojoUtils {
27  
28  	public static SpringService getService(String serviceClassname) {
29  		try {
30  			Class<?> serviceClass = Class.forName(serviceClassname);
31  			return (SpringService) serviceClass.newInstance();
32  		} catch (ClassNotFoundException e) {
33  			throw new IllegalStateException("Unexpected error", e);
34  		} catch (IllegalAccessException e) {
35  			throw new IllegalStateException("Unexpected error", e);
36  		} catch (InstantiationException e) {
37  			throw new IllegalStateException("Unexpected error", e);
38  		}
39  	}
40  
41  	public static Properties getMavenProperties(MavenProject project, Properties mojoProperties) {
42  		// Get internal Maven config as a properties object
43  		Properties internal = MojoUtils.getInternalProperties(project);
44  		// The ordering here is significant.
45  		// Properties supplied directly to the mojo override properties from project.getProperties()
46  		// But, internal Maven properties need to always win.
47  		// ${project.artifactId} needs to always faithfully represent the correct artifactId
48  		Properties properties = PropertyUtils.combine(project.getProperties(), mojoProperties, internal);
49  		// Explicitly override internal Maven props with system/env props (simulates the default maven behavior)
50  		PropertyUtils.overrideWithGlobalValues(properties, GlobalPropertiesMode.BOTH);
51  		// Return the overridden properties
52  		return properties;
53  	}
54  
55  	public static Properties getInternalProperties(MavenProject project) {
56  		Properties properties = new Properties();
57  		properties.setProperty("project.id", project.getId());
58  		properties.setProperty("project.groupId", project.getGroupId());
59  		properties.setProperty("project.artifactId", project.getArtifactId());
60  		properties.setProperty("project.version", project.getVersion());
61  		properties.setProperty("project.packaging", project.getPackaging());
62  		properties.setProperty("project.name", project.getName());
63  		properties.setProperty("project.description", project.getDescription());
64  		properties.setProperty("project.inceptionYear", project.getInceptionYear());
65  		properties.setProperty("project.ciManagement.system", project.getCiManagement().getSystem());
66  		properties.setProperty("project.ciManagement.url", project.getCiManagement().getUrl());
67  		properties.setProperty("project.issueManagement.system", project.getIssueManagement().getSystem());
68  		properties.setProperty("project.issueManagement.url", project.getIssueManagement().getUrl());
69  		properties.setProperty("project.basedir", LocationUtils.getCanonicalPath(project.getBasedir()));
70  		properties.setProperty("project.build.directory", project.getBuild().getDirectory());
71  		properties.setProperty("project.build.outputDirectory", project.getBuild().getOutputDirectory());
72  		properties.setProperty("project.build.testOutputDirectory", project.getBuild().getTestOutputDirectory());
73  		properties.setProperty("project.build.sourceDirectory", project.getBuild().getSourceDirectory());
74  		properties.setProperty("project.build.scriptSourceDirectory", project.getBuild().getScriptSourceDirectory());
75  		properties.setProperty("project.build.testSourceDirectory", project.getBuild().getTestSourceDirectory());
76  		return properties;
77  	}
78  }