Skip to content
Merged
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
18 changes: 16 additions & 2 deletions linkage-monitor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/Linkage-Monito
# Installation

This tool works as part of presubmit checks in the library projects in GitHub. The check
fails the build when code or a dependency changes in such a way as to introduce a new linkage error in
the [Google Cloud Libraries BOM](../README.md#google-libraries-bom);
fails the build when code or a dependency changes in such a way as to introduce a new linkage error
in the [Google Cloud Libraries BOM](../README.md#google-libraries-bom);

Example presubmit build script:

Expand All @@ -36,3 +36,17 @@ is `cloud-opensource-java/ubuntu/linkage-monitor-gcs.sh`.

The Kokoro config lives in google3 at
`google3/devtools/kokoro/config/prod/cloud-opensource-java/ubuntu/linkage-monitor-gcs.cfg`

# Debugging Linkage Monitor

In case you need to get debug messages from Linkage Monitor, prepare the following file as
`logging.properties`:

```
.level = INFO
handlers= java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = FINE
com.google.cloud.tools.dependencies.linkagemonitor.level = FINE
```

and pass a system property `-Djava.util.logging.config.file=logging.properties` to JVM.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -144,13 +145,14 @@ private static void printUsageAndDie() {
}

/**
* Returns a map from versionless coordinates to version for all pom.xml found in {@code
* projectDirectory}.
* Returns a map from versionless coordinates to the versions of the Maven coordinates in all
* pom.xml and the {@code dependencyManagement} section of BOMs found in {@code projectDirectory}.
*/
@VisibleForTesting
static ImmutableMap<String, String> findLocalArtifacts(
RepositorySystem repositorySystem, RepositorySystemSession session, Path projectDirectory) {
ImmutableMap.Builder<String, String> artifactToVersion = ImmutableMap.builder();
// The same coordinates may be added multiple times by an artifact and an entry in a BOM.
Map<String, String> artifactToVersion = new HashMap<>();
Iterable<Path> paths = MoreFiles.fileTraverser().breadthFirst(projectDirectory);

for (Path path : paths) {
Expand Down Expand Up @@ -188,14 +190,28 @@ static ImmutableMap<String, String> findLocalArtifacts(
try {
ModelBuildingResult modelBuildingResult = modelBuilder.build(modelRequest);
Model model = modelBuildingResult.getEffectiveModel();
artifactToVersion.put(model.getGroupId() + ":" + model.getArtifactId(), model.getVersion());
String versionlessCoordinates = model.getGroupId() + ":" + model.getArtifactId();
artifactToVersion.put(versionlessCoordinates, model.getVersion());
logger.fine("Found local artifact: " + model);
DependencyManagement dependencyManagement = model.getDependencyManagement();
if ("pom".equals(model.getPackaging()) && dependencyManagement != null) {
// Read the content of a BOM.
for (org.apache.maven.model.Dependency dependency :
dependencyManagement.getDependencies()) {
String managedDependencyVersionlessCoordinates =
dependency.getGroupId() + ":" + dependency.getArtifactId();
artifactToVersion.put(managedDependencyVersionlessCoordinates, dependency.getVersion());
logger.fine("Found local artifact in the BOM: " + dependency);
}
}

} catch (ModelBuildingException ex) {
// Maven may fail to build pom.xml files found in irrelevant directories, such as "target"
// and "test" directories of the project. Such failures can be ignored.
logger.info("Ignoring bad model: " + path + ": " + ex.getMessage());
}
}
return artifactToVersion.build();
return ImmutableMap.copyOf(artifactToVersion);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,16 @@ public void testFindLocalArtifacts() {
LinkageMonitor.findLocalArtifacts(
system, session, Paths.get("src/test/resources/testproject"));

// This should not include project under "build" directory
Truth.assertThat(localArtifacts).hasSize(2);
Truth.assertThat(localArtifacts).containsKey("com.google.cloud.tools:test-project");
Truth.assertThat(localArtifacts).containsKey("com.google.cloud.tools:test-subproject");
// This should not include project under "build" directory, but should include the entries
// in the dependencyManagement section of the gax-bom/pom.xml.
Truth.assertThat(localArtifacts).hasSize(6);
assertEquals("0.0.1-SNAPSHOT", localArtifacts.get("com.google.cloud.tools:test-project"));
assertEquals("0.0.2-SNAPSHOT", localArtifacts.get("com.google.cloud.tools:test-subproject"));
assertEquals("1.60.2-SNAPSHOT", localArtifacts.get("com.google.api:gax-bom"));
assertEquals(
"localArtifacts should contain the dependency management section in the BOM",
"1.60.2-SNAPSHOT",
localArtifacts.get("com.google.api:gax"));
}

@Test
Expand Down
47 changes: 47 additions & 0 deletions linkage-monitor/src/test/resources/testproject/gax-bom/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.api</groupId>
<artifactId>gax-bom</artifactId>
<version>1.60.2-SNAPSHOT</version>
<packaging>pom</packaging>
<name>GAX (Google Api eXtensions) for Java</name>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this metadata isn't needed for the test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Removed the unnecessary metadata.

<description>GAX BOM version 1.60.2-SNAPSHOT copied for Linkage Monitor test</description>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
<version>1.60.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
<version>1.60.2-SNAPSHOT</version>
<classifier>testlib</classifier>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax-grpc</artifactId>
<version>1.60.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax-grpc</artifactId>
<version>1.60.2-SNAPSHOT</version>
<classifier>testlib</classifier>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax-httpjson</artifactId>
<version>0.77.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax-httpjson</artifactId>
<version>0.77.2-SNAPSHOT</version>
<classifier>testlib</classifier>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.google.cloud.tools</groupId>
<artifactId>test-subproject</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>

<name>Test Sub Project for LinkageMonitorTest</name>

Expand Down