Skip to content

Commit

Permalink
Improved maven integration with test projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbegaudeau committed Jan 17, 2012
1 parent e874119 commit 9376c87
Show file tree
Hide file tree
Showing 27 changed files with 1,198 additions and 15 deletions.
5 changes: 5 additions & 0 deletions maven/org.eclipse.acceleo.maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>parser</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.acceleo</groupId>
<artifactId>model</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.acceleo</groupId>
<artifactId>common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@

import java.io.File;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.eclipse.acceleo.common.internal.utils.AcceleoPackageRegistry;
import org.eclipse.acceleo.internal.parser.compiler.AcceleoParser;
import org.eclipse.acceleo.internal.parser.compiler.AcceleoProjectClasspathEntry;
Expand All @@ -38,11 +46,19 @@
*
* @goal acceleo-compile
* @phase compile
* @requiresDependencyResolution runtime
* @author <a href="mailto:[email protected]">Stephane Begaudeau</a>
* @since 3.2
*/
public class AcceleoParserMojo extends AbstractMojo {

/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;

/**
* Indicates if we are compiling the Acceleo modules as binary resources.
*
Expand Down Expand Up @@ -74,39 +90,49 @@ public class AcceleoParserMojo extends AbstractMojo {
*/
public void execute() throws MojoExecutionException, MojoFailureException {
Log log = getLog();
log.info("ACCELEO MAVEN STAND ALONE BUILD");
log.info("Acceleo maven stand alone build...");

log.info("Registering packages...");
log.info("Starting packages registration...");
for (String packageToRegister : this.packagesToRegister) {
try {
Class<?> forName = Class.forName(packageToRegister);
List<?> runtimeClasspathElements = project.getRuntimeClasspathElements();
URL[] runtimeUrls = new URL[runtimeClasspathElements.size()];
for (int i = 0; i < runtimeClasspathElements.size(); i++) {
String element = (String)runtimeClasspathElements.get(i);
runtimeUrls[i] = new File(element).toURI().toURL();
}
URLClassLoader newLoader = new URLClassLoader(runtimeUrls, Thread.currentThread()
.getContextClassLoader());

Class<?> forName = Class.forName(packageToRegister, true, newLoader);
Field nsUri = forName.getField("eNS_URI");
Field eInstance = forName.getField("eINSTANCE");

Object newInstance = forName.newInstance();
Object nsURIInvoked = nsUri.get(newInstance);
Object nsURIInvoked = nsUri.get(null);
if (nsURIInvoked instanceof String) {
log.info("Registering package '" + packageToRegister + "'.");
AcceleoPackageRegistry.INSTANCE.put((String)nsURIInvoked, eInstance.get(newInstance));
AcceleoPackageRegistry.INSTANCE.put((String)nsURIInvoked, eInstance.get(null));
} else {
log.error("The URI field is not a string.");
}

} catch (ClassNotFoundException e) {
log.error(e);
} catch (InstantiationException e) {
log.error(e);
} catch (IllegalAccessException e) {
log.error(e);
} catch (SecurityException e) {
log.error(e);
} catch (NoSuchFieldException e) {
log.error(e);
} catch (DependencyResolutionRequiredException e) {
log.error(e);
} catch (MalformedURLException e) {
log.error(e);
}

}

log.info("Starting the build sequence...");
log.info("Starting the build sequence for the project '" + this.acceleoProject.getRoot() + "'...");
log.info("Mapping the pom.xml to AcceleoProject...");

Preconditions.checkNotNull(this.acceleoProject);
Expand All @@ -123,6 +149,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
for (Entry entry : entries) {
File inputDirectory = new File(root, entry.getInput());
File outputDirectory = new File(root, entry.getOutput());

log.debug("Input: " + inputDirectory.getAbsolutePath());
log.debug("Output: " + outputDirectory.getAbsolutePath());

AcceleoProjectClasspathEntry classpathEntry = new AcceleoProjectClasspathEntry(inputDirectory,
outputDirectory);
classpathEntries.add(classpathEntry);
Expand Down Expand Up @@ -154,12 +184,92 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

log.info("Adding jar dependencies...");
List<File> jars = this.acceleoProject.getJars();
List<String> jars = this.acceleoProject.getJars();
if (jars != null) {
Set<URI> newDependencies = new LinkedHashSet<URI>();
for (File jar : jars) {
URI uri = URI.createFileURI(jar.getAbsolutePath());
newDependencies.add(uri);
for (String jar : jars) {
log.info("Resolving jar: '" + jar + "'...");
File jarFile = new File(jar);
if (jarFile.isFile()) {
URI uri = URI.createFileURI(jar);
newDependencies.add(uri);
log.info("Found jar for '" + jar + "' on the filesystem: '" + jarFile.getAbsolutePath()
+ "'.");
} else {
StringTokenizer tok = new StringTokenizer(jar, ":");

String groupdId = null;
String artifactId = null;
String version = null;

int c = 0;
while (tok.hasMoreTokens()) {
String nextToken = tok.nextToken();
if (c == 0) {
groupdId = nextToken;
} else if (c == 1) {
artifactId = nextToken;
} else if (c == 2) {
version = nextToken;
}

c++;
}

Set<?> artifacts = this.project.getArtifacts();
for (Object object : artifacts) {
if (object instanceof Artifact) {
Artifact artifact = (Artifact)object;
if (groupdId != null && groupdId.equals(artifact.getGroupId())
&& artifactId != null && artifactId.equals(artifact.getArtifactId())) {
if (version != null && version.equals(artifact.getVersion())) {
File artifactFile = artifact.getFile();
if (artifactFile != null && artifactFile.exists()) {
URI uri = URI.createFileURI(artifactFile.getAbsolutePath());
newDependencies.add(uri);
log.info("Found jar for '" + jar + "' on the filesystem: '"
+ uri.toString() + "'.");
}
} else if (version == null) {
File artifactFile = artifact.getFile();
if (artifactFile != null && artifactFile.exists()) {
URI uri = URI.createFileURI(artifactFile.getAbsolutePath());
newDependencies.add(uri);
log.info("Found jar for '" + jar + "' on the filesystem: '"
+ uri.toString() + "'.");
}
}
}
}
}

List<?> mavenDependencies = this.project.getDependencies();
for (Object object : mavenDependencies) {
if (object instanceof Dependency) {
Dependency dependency = (Dependency)object;
if (groupdId != null && groupdId.equals(dependency.getGroupId())
&& artifactId != null && artifactId.equals(dependency.getArtifactId())) {
if (version != null && version.equals(dependency.getVersion())) {
String systemPath = dependency.getSystemPath();
if (systemPath != null && new File(systemPath).exists()) {
URI uri = URI.createFileURI(systemPath);
newDependencies.add(uri);
log.info("Found jar for '" + jar + "' on the filesystem: '"
+ uri.toString() + "'.");
}
} else if (version == null) {
String systemPath = dependency.getSystemPath();
if (systemPath != null && new File(systemPath).exists()) {
URI uri = URI.createFileURI(systemPath);
newDependencies.add(uri);
log.info("Found jar for '" + jar + "' on the filesystem: '"
+ uri.toString() + "'.");
}
}
}
}
}
}
}
aProject.addDependencies(newDependencies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AcceleoProject {
/**
* The jars.
*/
private List<File> jars;
private List<String> jars;

/**
* The dependencies.
Expand Down Expand Up @@ -64,7 +64,7 @@ public List<AcceleoProject> getDependencies() {
*
* @return The jar dependencies.
*/
public List<File> getJars() {
public List<String> getJars() {
return jars;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/org.eclipse.acceleo.maven.plugin.tests/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions tests/org.eclipse.acceleo.maven.plugin.tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
34 changes: 34 additions & 0 deletions tests/org.eclipse.acceleo.maven.plugin.tests/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.acceleo.maven.plugin.tests</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Mon Jan 16 16:02:24 CET 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Mon Jan 16 16:02:24 CET 2012
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Tests
Bundle-SymbolicName: org.eclipse.acceleo.maven.plugin.tests
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.obeonetwork.pim.uml2.gen.java;bundle-version="1.0.0"
5 changes: 5 additions & 0 deletions tests/org.eclipse.acceleo.maven.plugin.tests/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source.. = src/main/java/
output.. = target/classes/
bin.includes = META-INF/,\
.
jre.compilation.profile = J2SE-1.5
Empty file.
Loading

0 comments on commit 9376c87

Please sign in to comment.