View Javadoc

1   package org.rhq.maven.plugins;
2   
3   import static org.codehaus.plexus.util.StringUtils.join;
4   import static org.rhq.maven.plugins.Utils.redirectOuput;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.util.Collections;
9   import java.util.Iterator;
10  import java.util.LinkedList;
11  import java.util.List;
12  
13  import org.apache.maven.artifact.Artifact;
14  import org.apache.maven.artifact.factory.ArtifactFactory;
15  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
16  import org.apache.maven.artifact.repository.ArtifactRepository;
17  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
18  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
19  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
20  import org.apache.maven.artifact.resolver.ArtifactResolver;
21  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugins.annotations.Component;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  
29  @Mojo(name = "generatorProject", requiresProject=false)
30  public class GeneratorProjectMojo extends AbstractMojo {
31  	
32  	private static final String PLUGIN_GENERATOR_MODULE_GROUP_ID = "org.rhq.helpers";
33  	private static final String PLUGIN_GENERATOR_MODULE_ARTIFACT_ID = "rhq-pluginGen";
34  	private static final String PLUGIN_GENERATOR_MAIN_CLASS = "org.rhq.helpers.pluginGen.PluginGen";
35  	//TODO
36  	private static final String PLUGIN_GENERATOR_VERSION = "3.0.4";
37  	
38  	@Parameter(defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true)
39  	private List remoteRepositories;
40  
41  	@Parameter(defaultValue = "${localRepository}", required = true, readonly = true)
42  	private ArtifactRepository localRepository;
43  
44  	@Component
45  	private ArtifactFactory artifactFactory;
46  	
47  	@Component
48  	private ArtifactResolver artifactResolver;
49  	
50  	@Component
51  	private ArtifactMetadataSource artifactMetadataSource;
52  	
53  	@Override
54  	public void execute() throws MojoExecutionException, MojoFailureException {
55  		System.out.println("Generate Project Mojo rhq maven plugins");
56  		Process process = null;
57  		try {
58  			String pluginValidatorClasspath = buildPluginValidatorClasspath();
59  			String javaCommand = buildJavaCommand();
60  			ProcessBuilder processBuilder = buildProcessBuilder(javaCommand,
61  					pluginValidatorClasspath);
62  			process = processBuilder.start();
63  			redirectOuput(process);
64  			int exitCode = process.waitFor();
65  			if (exitCode != 0) {
66  				handleFailure("Invalid plugin");
67  			}
68  		} catch (Exception e) {
69  			handleException(e);
70  		} finally {
71  			if (process != null) {
72  				process.destroy();
73  			}
74  		}
75  	}
76  	
77  	private String buildPluginValidatorClasspath() throws ArtifactNotFoundException,
78  			ArtifactResolutionException, IOException {
79  		// Resolve dependencies of the plugin validator module
80  		Artifact dummyOriginatingArtifact = artifactFactory
81  				.createBuildArtifact("org.apache.maven.plugins",
82  						"maven-downloader-plugin", "1.0", "jar");
83  		Artifact pluginContainerArtifact = this.artifactFactory.createArtifact(
84  				PLUGIN_GENERATOR_MODULE_GROUP_ID,
85  				PLUGIN_GENERATOR_MODULE_ARTIFACT_ID, PLUGIN_GENERATOR_VERSION, null, "jar");
86  		ArtifactResolutionResult artifactResolutionResult = artifactResolver
87  				.resolveTransitively(
88  						Collections.singleton(pluginContainerArtifact),
89  						dummyOriginatingArtifact, localRepository,
90  						remoteRepositories, artifactMetadataSource, null);
91  
92  		List<String> classpathElements = new LinkedList<String>();
93  
94  		// Add all compile scope dependencies to the classpath
95  		Iterator iterator = artifactResolutionResult.getArtifacts().iterator();
96  		ScopeArtifactFilter artifactFilter = new ScopeArtifactFilter(
97  				Artifact.SCOPE_COMPILE);
98  		while (iterator.hasNext()) {
99  			Artifact artifact = (Artifact) iterator.next();
100 			if (!artifact.isOptional() && artifact.getType().equals("jar")
101 					&& artifactFilter.include(artifact)) {
102 				classpathElements.add(artifact.getFile().getAbsolutePath());
103 			}
104 		}
105 
106 		String pluginGeneratorClasspath = join(classpathElements.iterator(),
107 				File.pathSeparator);
108 
109 		if (getLog().isDebugEnabled()) {
110 			getLog().debug(
111 					"pluginGeneratorClasspath = " + pluginGeneratorClasspath);
112 		}
113 
114 		return pluginGeneratorClasspath;
115 	}
116 	private String buildJavaCommand() {
117 		String javaHome = System.getProperty("java.home");
118 		String javaCommand = javaHome + File.separator + "bin" + File.separator
119 				+ "java";
120 		if (getLog().isDebugEnabled()) {
121 			getLog().debug("javaCommand = " + javaCommand);
122 		}
123 		return javaCommand;
124 	}
125 	private ProcessBuilder buildProcessBuilder(String javaCommand,
126 			String pluginValidatorClasspath) {
127 		List<String> command = new LinkedList<String>();
128 		command.add(javaCommand);
129 		command.add("-classpath");
130 		command.add(pluginValidatorClasspath);
131 		command.add("-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog");
132 		command.add(PLUGIN_GENERATOR_MAIN_CLASS);
133 		if (getLog().isDebugEnabled()) {
134 			getLog().debug(
135 					"Built command to execute = "
136 							+ join(command.iterator(), " "));
137 		}
138 		return new ProcessBuilder().directory(new File(System.getProperty("user.dir"))).command(command);
139 	}
140 	
141 	
142 	private void handleFailure(String message) throws MojoFailureException {
143 //		if (failOnError) {
144 //			throw new MojoFailureException(message);
145 //		}
146 		getLog().error(message);
147 	}
148 
149 	private void handleException(Exception e) throws MojoExecutionException {
150 //		if (failOnError) {
151 //			throw new MojoExecutionException(e.getMessage(), e);
152 //		}
153 		getLog().error(e.getMessage(), e);
154 	}
155 }