Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to output dependencies as a JSON file. #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions depends-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<artifactId>depends-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.4.1-SNAPSHOT</version>
<version>1.5.0-SNAPSHOT</version>
<name>Apache ServiceMix :: Plugins :: Maven2 Depends Plugin</name>

<scm>
Expand Down Expand Up @@ -64,13 +64,18 @@
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0</version>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-build-api</artifactId>
<version>0.0.7</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
*/
package org.apache.servicemix.tooling.depends;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
Expand All @@ -33,14 +29,16 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import org.sonatype.plexus.build.incremental.BuildContext;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.*;

/**
* Generates the dependencies properties file
*/
Expand Down Expand Up @@ -77,6 +75,12 @@ public class GenerateDependsFileMojo extends AbstractMojo {
@Parameter( defaultValue = "${project.build.directory}/classes/META-INF/maven/dependencies.properties" )
private File outputFile;

@Parameter( defaultValue = "${project.build.directory}/classes/META-INF/maven/dependencies.json" )
private File outputJsonFile;

@Parameter( defaultValue = "false", property = "outputAsJson")
protected boolean outputAsJson;

@Parameter( defaultValue = "${localRepository}" )
protected ArtifactRepository localRepo;

Expand Down Expand Up @@ -140,6 +144,9 @@ private List<Dependency> getDependencies() {
private void writeDependencies(List<Dependency> dependencies) throws MojoExecutionException {
OutputStream out = null;
try {
if(outputAsJson) {
outputFile = outputJsonFile;
}
outputFile.getParentFile().mkdirs();
out = buildContext.newFileOutputStream(outputFile);
PrintStream printer = new PrintStream(out);
Expand Down Expand Up @@ -170,7 +177,15 @@ protected Dependency generateDependency(Artifact a)
return dep;
}

protected void populateProperties(PrintStream out, List<Dependency> dependencies) {
protected void populateProperties(PrintStream printer, List<Dependency> dependencies) {
if(outputAsJson) {
populatePropertiesIntoJson(printer, dependencies);
} else {
populatePropertiesIntoText(printer, dependencies);
}
}

protected void populatePropertiesIntoText(PrintStream out, List<Dependency> dependencies) {
out.println("# Project dependencies generated by the Apache ServiceMix Maven Plugin");
out.println();

Expand All @@ -184,7 +199,7 @@ protected void populateProperties(PrintStream out, List<Dependency> dependencies

for (Dependency dependency : dependencies) {
String prefix = dependency.getGroupId() + SEPARATOR + dependency.getArtifactId() + SEPARATOR;

if( includeVersion )
out.println(prefix + "version = " + dependency.getVersion());

Expand All @@ -203,6 +218,24 @@ protected void populateProperties(PrintStream out, List<Dependency> dependencies
getLog().debug("Dependency: " + dependency + " classifier: " + classifier + " type: " + dependency.getType());
}
}

protected void populatePropertiesIntoJson(PrintStream out, List<Dependency> dependencies) {
PomOutput output = new PomOutput();

Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();

output.project = new HashMap<String, String>();
output.project.put("groupId", project.getGroupId());
output.project.put("artifactId", project.getArtifactId());
output.project.put("versionId", project.getVersion());

output.dependencies = new ArrayList<Dependency>();
output.dependencies.addAll(dependencies);

out.println(gson.toJson(output));
}

private void safeClose(OutputStream out) {
if (out != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.apache.servicemix.tooling.depends;

import org.apache.maven.model.Dependency;

import java.util.HashMap;
import java.util.List;

public class PomOutput {

HashMap<String, String> project;

List<Dependency> dependencies;

}