Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#782: Make hawtio:camel-blueprint auto discover needed camel-test-blu…
…eprint dependencies on-the-fly.
  • Loading branch information
davsclaus committed Nov 30, 2013
1 parent 18ef0a3 commit 7e97f73
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
60 changes: 18 additions & 42 deletions hawtio-maven-plugin/src/main/java/io/hawt/maven/BaseMojo.java
Expand Up @@ -37,40 +37,37 @@ public abstract class BaseMojo extends AbstractMojo {
private long daemonThreadJoinTimeout = 15000L;

@Component
private MavenProject project;
MavenProject project;

@Component
private ArtifactResolver artifactResolver;
ArtifactResolver artifactResolver;

@Component
private ArtifactFactory artifactFactory;
ArtifactFactory artifactFactory;

@Component
private MavenProjectBuilder projectBuilder;
MavenProjectBuilder projectBuilder;

@Component
private ArtifactMetadataSource metadataSource;
ArtifactMetadataSource metadataSource;

@Parameter(property = "localRepository", readonly = true, required = true)
private ArtifactRepository localRepository;
ArtifactRepository localRepository;

@Parameter(property = "project.remoteArtifactRepositories")
private List<?> remoteRepositories;
List<?> remoteRepositories;

@Parameter(readonly = true, property = "plugin.artifacts")
private List<Artifact> pluginDependencies;
List<Artifact> pluginDependencies;

@Parameter(readonly = true, property = "project.dependencyArtifacts")
private Set<Artifact> projectDependencies;
Set<Artifact> projectDependencies;

@Parameter(property = "hawtio.logClasspath", defaultValue = "false")
private boolean logClasspath;
boolean logClasspath;

@Parameter(property = "hawtio.logDependencies", defaultValue = "false")
private boolean logDependencies;

private boolean includeProjectDependencies = true;
private boolean includePluginDependencies = false;
boolean logDependencies;

String extraPluginDependencyArtifactId;
String extendedPluginDependencyArtifactId;
Expand Down Expand Up @@ -121,6 +118,10 @@ protected Set<Artifact> resolveArtifacts() throws Exception {
}
}

return artifacts;
}

protected void resolvedArtifacts(Set<Artifact> artifacts) throws Exception {
if (logDependencies) {
List<Artifact> sorted = new ArrayList<Artifact>(artifacts);
Collections.sort(sorted);
Expand All @@ -129,7 +130,6 @@ protected Set<Artifact> resolveArtifacts() throws Exception {
getLog().info(" " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getType() + ":" + artifact.getVersion() + ":" + artifact.getScope());
}
}
return artifacts;
}

/**
Expand Down Expand Up @@ -166,8 +166,7 @@ protected void addRelevantProjectDependencies(Set<Artifact> artifacts) throws Ex
Iterator<Artifact> iter = dependencies.iterator();
while (iter.hasNext()) {
Artifact classPathElement = iter.next();
getLog().debug("Adding project dependency artifact: " + classPathElement.getArtifactId()
+ " to classpath");
getLog().debug("Adding project dependency artifact: " + classPathElement.getArtifactId() + " to classpath");

artifacts.add(classPathElement);
}
Expand All @@ -187,21 +186,13 @@ protected void addExtraPluginDependencies(Set<Artifact> artifacts) throws MojoEx
// must
if (artifact.getArtifactId().equals(extraPluginDependencyArtifactId)
|| artifact.getArtifactId().equals(extendedPluginDependencyArtifactId)) {
getLog().debug("Adding extra plugin dependency artifact: " + artifact.getArtifactId()
+ " to classpath");

getLog().debug("Adding extra plugin dependency artifact: " + artifact.getArtifactId() + " to classpath");
artifacts.add(artifact);

// add the transient dependencies of this artifact
Set<Artifact> resolvedDeps = resolveExecutableDependencies(artifact);
for (Artifact dep : resolvedDeps) {

// we must skip org.apache.aries.blueprint.core:, otherwise we get duplicate blueprint extenders
if (dep.getArtifactId().equals("org.apache.aries.blueprint.core")) {
getLog().debug("Skipping org.apache.aries.blueprint.core -> " + dep.getGroupId() + "/" + dep.getArtifactId() + "/" + dep.getVersion());
continue;
}

getLog().debug("Adding extra plugin dependency artifact: " + dep.getArtifactId() + " to classpath");
artifacts.add(dep);
}
Expand All @@ -213,28 +204,13 @@ protected void addExtraPluginDependencies(Set<Artifact> artifacts) throws MojoEx
* Add any relevant project dependencies to the classpath.
*/
protected void addRelevantPluginDependencies(Set<Artifact> artifacts) throws MojoExecutionException {
if (pluginDependencies == null || !includePluginDependencies) {
if (pluginDependencies == null) {
return;
}

Iterator<Artifact> iter = this.pluginDependencies.iterator();
while (iter.hasNext()) {
Artifact classPathElement = iter.next();

// we must skip org.osgi.core, otherwise we get a
// java.lang.NoClassDefFoundError: org.osgi.vendor.framework property not set
if (classPathElement.getArtifactId().equals("org.osgi.core")) {
getLog().info("Skipping org.osgi.core -> " + classPathElement.getGroupId() + "/" + classPathElement.getArtifactId() + "/" + classPathElement.getVersion());
continue;
}

// we must skip org.apache.aries.blueprint, otherwise we get a
// java.lang.NoClassDefFoundError: org.osgi.vendor.framework property not set
if (classPathElement.getArtifactId().equals("org.apache.aries.blueprint")) {
getLog().info("Skipping org.apache.aries.blueprint -> " + classPathElement.getGroupId() + "/" + classPathElement.getArtifactId() + "/" + classPathElement.getVersion());
continue;
}

getLog().debug("Adding plugin dependency artifact: " + classPathElement.getArtifactId() + " to classpath");
artifacts.add(classPathElement);
}
Expand Down
Expand Up @@ -43,6 +43,15 @@ protected void addCustomArguments(List<String> args) {
}
}

protected Artifact getCamelCoreArtifact(Set<Artifact> artifacts) throws MojoExecutionException {
for (Artifact artifact : artifacts) {
if (artifact.getGroupId().equals("org.apache.camel") && artifact.getArtifactId().equals("camel-core")) {
return artifact;
}
}
return null;
}

protected Artifact getCamelBlueprintArtifact(Set<Artifact> artifacts) throws MojoExecutionException {
for (Artifact artifact : artifacts) {
if (artifact.getGroupId().equals("org.apache.camel") && artifact.getArtifactId().equals("camel-test-blueprint")) {
Expand All @@ -54,10 +63,33 @@ protected Artifact getCamelBlueprintArtifact(Set<Artifact> artifacts) throws Moj

@Override
protected void resolvedArtifacts(Set<Artifact> artifacts) throws Exception {
Artifact camelCore = getCamelBlueprintArtifact(artifacts);
Artifact camelCore = getCamelCoreArtifact(artifacts);
if (camelCore == null) {
throw new IllegalAccessError("Cannot resolve camel-test-blueprint dependency from the Maven pom.xml file");
throw new IllegalAccessError("Cannot resolve camel-core dependency from the Maven pom.xml file");
}

// try to find camel-test-blueprint which we need
Artifact camelTestBlueprint = getCamelBlueprintArtifact(artifacts);
if (camelTestBlueprint == null) {
camelTestBlueprint = artifactFactory.createArtifact("org.apache.camel", "camel-test-blueprint", camelCore.getVersion(), null, "jar");
Set<Artifact> extras = resolveExecutableDependencies(camelTestBlueprint);
if (extras.isEmpty()) {
throw new IllegalAccessError("Cannot resolve camel-test-blueprint dependency from the Maven pom.xml file");
}

for (Artifact extra : extras) {
getLog().debug("Extra artifact: " + extra);
if (Artifact.SCOPE_TEST.equals(extra.getScope())) {
continue;
}
if (!artifacts.contains(extra)) {
getLog().debug("Adding extra artifact: " + extra);
artifacts.add(extra);
}
}
}

super.resolvedArtifacts(artifacts);
}

}
Expand Up @@ -56,11 +56,14 @@ protected void resolvedArtifacts(Set<Artifact> artifacts) throws Exception {
if (camelCore == null) {
throw new IllegalAccessError("Cannot resolve camel-core dependency from the Maven pom.xml file");
}

super.resolvedArtifacts(artifacts);
}

@Override
protected boolean filterUnwantedArtifacts(Artifact artifact) {
// use camel-blueprint goal for blueprint/osgi
// filter out unwanted OSGi related JARs as some projects like ActiveMQ includes these dependencies
// and you should use the camel-blueprint goal for running as OSGi
if (artifact.getGroupId().equals("org.apache.aries.blueprint")) {
return true;
} else if (artifact.getGroupId().startsWith("org.ops4j")) {
Expand Down
8 changes: 1 addition & 7 deletions hawtio-maven-plugin/src/main/java/io/hawt/maven/RunMojo.java
Expand Up @@ -34,8 +34,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// use hawtio-app
extendedPluginDependencyArtifactId = "hawtio-app";

getLog().info("hawtio web console at http://localhost:" + port + "/" + context);

try {
doPrepareArguments();
doExecute();
Expand Down Expand Up @@ -99,7 +97,7 @@ public void run() {
getLog().debug("Setting accessibility to true in order to invoke main().");
hawtioMain.setAccessible(true);
}
String[] args = new String[]{"--port", "" + port, "--join", "false"};
String[] args = new String[]{"--context", context, "--port", "" + port, "--join", "false"};
hawtioMain.invoke(hawtioMain, new Object[]{args});

afterBootstrapHawtio();
Expand Down Expand Up @@ -150,10 +148,6 @@ public void run() {
}
}

protected void resolvedArtifacts(Set<Artifact> artifacts) throws Exception {
// noop
}

protected void beforeBootstrapMain() {
// noop
}
Expand Down

0 comments on commit 7e97f73

Please sign in to comment.