-
Notifications
You must be signed in to change notification settings - Fork 29
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
Maven plugin for dumping dependencies (cross-repo navigation) #735
Merged
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b4a04b6
WIP Maven plugin for dumping dependencies
antonsviridov-src b4c13bf
remove version override
antonsviridov-src 485db58
Add `build` command which packages CLI in a reasonable location
antonsviridov-src d4f1f02
update SBT
antonsviridov-src 28c65c5
Clean up the maven plugin
antonsviridov-src 2c3b1ca
move benchmark tests to check job
antonsviridov-src 65f18a2
Add a new maven workflow
antonsviridov-src 0db33d6
Add a Maven example with plugin enabled
antonsviridov-src 3907bb8
Run in correct folder
antonsviridov-src 6787e9f
Use absolute path
antonsviridov-src 6a53c05
use env variables instead
antonsviridov-src 4a0a611
Run extra checks on the outputs
antonsviridov-src f73c949
use "Maven CI friendly" versions
antonsviridov-src 46d43ab
Fix artifact rendering in error message
antonsviridov-src 9b5af9d
Write dependencies files unique identified by the maven project
antonsviridov-src 81ceda0
Read classpath entries from possibly any number of *dependencies.txt
antonsviridov-src c931481
Add documentation for maven plugin
antonsviridov-src 15a9669
formatting
antonsviridov-src 8604307
Relax path matching in maven CI
antonsviridov-src 3967d26
Remove print
antonsviridov-src File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.sourcegraph</groupId> | ||
<artifactId>example</artifactId> | ||
<version>${revision}</version> | ||
|
||
<name>example</name> | ||
<!-- FIXME change it to the project's website --> | ||
<url>http://www.example.com</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<revision>1.0.0-SNAPSHOT</revision> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sourcegraph</groupId> | ||
<artifactId>semanticdb-javac</artifactId> | ||
<version>${scip-java.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> | ||
<plugins> | ||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> | ||
<plugin> | ||
<artifactId>maven-clean-plugin</artifactId> | ||
<version>3.1.0</version> | ||
</plugin> | ||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.0</version> | ||
<configuration> | ||
<compilerArgs> | ||
<arg>-Xplugin:semanticdb -sourceroot:${session.executionRootDirectory} -targetroot:${session.executionRootDirectory}/target/semanticdb-targetroot</arg> | ||
</compilerArgs> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<version>2.5.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>2.8.2</version> | ||
</plugin> | ||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> | ||
<plugin> | ||
<artifactId>maven-site-plugin</artifactId> | ||
<version>3.7.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-project-info-reports-plugin</artifactId> | ||
<version>3.0.0</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.sourcegraph</groupId> | ||
<artifactId>maven-plugin</artifactId> | ||
<version>${scip-java.version}</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>sourcegraphDependencies</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<!-- <configuration> --> | ||
<!-- <scope>test</scope> --> | ||
<!-- </configuration> --> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package test; | ||
|
||
/** | ||
* Hello world! | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
System.out.println( "Hello World!" ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Unit test for simple App. | ||
*/ | ||
public class AppTest | ||
{ | ||
/** | ||
* Rigorous Test :-) | ||
*/ | ||
@Test | ||
public void shouldAnswerWithTrue() | ||
{ | ||
assertTrue( true ); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
maven-plugin/src/main/java/com/sourcegraph/maven/DependencyWriterMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.sourcegraph.maven; | ||
|
||
import org.apache.maven.artifact.Artifact; | ||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
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.project.MavenProject; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Mojo( | ||
name = "sourcegraphDependencies", | ||
defaultPhase = LifecyclePhase.COMPILE, | ||
requiresDependencyResolution = ResolutionScope.COMPILE, | ||
requiresProject = true) | ||
public class DependencyWriterMojo extends AbstractMojo { | ||
@Parameter(defaultValue = "${project}", required = true, readonly = true) | ||
MavenProject project; | ||
|
||
@Parameter( | ||
property = "semanticdb.targetRoot", | ||
defaultValue = "${session.executionRootDirectory}/target/semanticdb-targetroot") | ||
private String targetRoot; | ||
|
||
public void execute() throws MojoExecutionException, MojoFailureException { | ||
Set artifacts = project.getArtifacts(); | ||
StringBuilder builder = new StringBuilder(); | ||
|
||
String groupID = project.getGroupId(); | ||
String artifactID = project.getArtifactId(); | ||
String version = project.getVersion(); | ||
List sourceRoots = project.getCompileSourceRoots(); | ||
|
||
if (groupID == null || artifactID == null) { | ||
getLog() | ||
.warn( | ||
"Failed to extract groupID and artifactID from the project.\n" | ||
+ "This will not prevent a SCIP index from being created, but the symbols \n" | ||
+ "extracted from this project won't be available for cross-repository navigation,\n" | ||
+ "as this project doesn't define any Maven coordinates by which it can be referred back to.\n" | ||
+ "See here for more details: https://sourcegraph.github.io/scip-java/docs/manual-configuration.html#step-5-optional-enable-cross-repository-navigation\n"); | ||
} else { | ||
for (Object root : sourceRoots) { | ||
if (root instanceof String) { | ||
String rootString = (String) root; | ||
builder.append( | ||
String.format("%s\t%s\t%s\t%s\n", groupID, artifactID, version, rootString)); | ||
} | ||
} | ||
} | ||
|
||
for (Object dep : artifacts) { | ||
if (dep instanceof Artifact) { | ||
Artifact artifact = (Artifact) dep; | ||
if (artifact.getFile() != null) { | ||
builder.append( | ||
String.format( | ||
"%s\t%s\t%s\t%s\n", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TSV ftw! |
||
artifact.getGroupId(), | ||
artifact.getArtifactId(), | ||
artifact.getVersion(), | ||
artifact.getFile())); | ||
} else { | ||
getLog() | ||
.warn( | ||
"Dependency " | ||
+ summariseArtifact(artifact) | ||
+ " does not have a resolved file, so it won't be added to the dependencies.txt"); | ||
} | ||
} | ||
} | ||
|
||
Path dependenciesFile = Paths.get(targetRoot).resolve("dependencies.txt"); | ||
|
||
try { | ||
Files.createDirectories(dependenciesFile.getParent()); | ||
try (BufferedWriter writer = Files.newBufferedWriter(dependenciesFile)) { | ||
writer.write(builder.toString()); | ||
} | ||
} catch (IOException e) { | ||
throw new MojoFailureException( | ||
"Failed to write dependencies to file " + dependenciesFile.toString(), e); | ||
} | ||
|
||
getLog().info("Dependencies were written to " + dependenciesFile.toAbsolutePath().toString()); | ||
} | ||
|
||
private String summariseArtifact(Artifact artifact) { | ||
return String.format( | ||
"%s:%s:%s", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maven has a special packaging mode for plugins, which seems to be nothing more than a post-processing step that introspects the plugin's pom.xml + all the Mojo files, then writes the special plugin.xml file.
https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
Setting up a Maven build just for that seems to be counterproductive, so we just write the
plugin.xml
by hand using a template. It's obviously not great, but at this point I don't see a better solution, and we don't expect this plugin to change very frequently.plugin.xml
will be force-kept in sync by tests, because Maven derives a lot of information from it, so if it's broken - things won't work.For local development this allows you to do
mavenPlugin/publishM2
and use that in maven builds.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I love sbt