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

Make the mojo define old & new classpaths automatically #266

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
63 changes: 52 additions & 11 deletions japicmp-maven-plugin/src/main/java/japicmp/maven/JApiCmpMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ Optional<XmlOutput> executeWithParameters(PluginParameters pluginParameters, Mav
getLog().warn("Skipping execution because no new version could be resolved/found.");
return Optional.absent();
}
if (jarArchiveComparator.getJarArchiveComparatorOptions().getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH) {
getLog().debug("comparing " + options.getOldArchives() + " to " + options.getNewArchives() + " using " + jarArchiveComparator.getCommonClasspathAsString());
} else {
getLog().debug("comparing " + options.getOldArchives() + " using " + jarArchiveComparator.getOldClassPathAsString() + " to " + options.getNewArchives() + " using " + jarArchiveComparator.getNewClassPathAsString());
}
List<JApiClass> jApiClasses = jarArchiveComparator.compare(options.getOldArchives(), options.getNewArchives());
try {
PostAnalysisScriptExecutor postAnalysisScriptExecutor = new PostAnalysisScriptExecutor();
Expand Down Expand Up @@ -593,7 +598,7 @@ private String createFilename(MavenParameters mavenParameters) {
return sb.toString();
}

private void setUpClassPath(JarArchiveComparatorOptions comparatorOptions, PluginParameters pluginParameters, MavenParameters mavenParameters) throws MojoFailureException {
private void setUpClassPath(JarArchiveComparatorOptions comparatorOptions, PluginParameters pluginParameters, MavenParameters mavenParameters) throws MojoFailureException, MojoExecutionException {
if (pluginParameters != null) {
if (pluginParameters.getDependenciesParam() != null) {
if (pluginParameters.getOldClassPathDependencies() != null || pluginParameters.getNewClassPathDependencies() != null) {
Expand Down Expand Up @@ -636,23 +641,59 @@ private void setUpClassPath(JarArchiveComparatorOptions comparatorOptions, Plugi
comparatorOptions.setClassPathMode(JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS);
} else {
if (getLog().isDebugEnabled()) {
getLog().debug("None of the elements <oldClassPathDependencies/>, <newClassPathDependencies/> or <dependencies/> found. Using " + JApiCli.ClassPathMode.ONE_COMMON_CLASSPATH);
getLog().debug("None of the elements <oldClassPathDependencies/>, <newClassPathDependencies/> or <dependencies/> found. Using " + JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS);
}
comparatorOptions.setClassPathMode(JarArchiveComparatorOptions.ClassPathMode.ONE_COMMON_CLASSPATH);
comparatorOptions.setClassPathMode(JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS);
}
}
}
setUpClassPathUsingMavenProject(comparatorOptions, mavenParameters, pluginParameters, ConfigurationVersion.NEW);
}

private void setUpClassPathUsingMavenProject(JarArchiveComparatorOptions comparatorOptions, MavenParameters mavenParameters, PluginParameters pluginParameters, ConfigurationVersion configurationVersion) throws MojoFailureException {
MavenProject mavenProject = mavenParameters.getMavenProject();
notNull(mavenProject, "Maven parameter mavenProject should be provided by maven container.");
private void setUpClassPathUsingMavenProject(JarArchiveComparatorOptions comparatorOptions, MavenParameters mavenParameters, PluginParameters pluginParameters, ConfigurationVersion configurationVersion) throws MojoFailureException, MojoExecutionException {
if (comparatorOptions.getClassPathMode() == JarArchiveComparatorOptions.ClassPathMode.TWO_SEPARATE_CLASSPATHS) {
if (pluginParameters.getOldVersionParam() != null) {
Dependency dependency = pluginParameters.getOldVersionParam().getDependency();
if (dependency != null) {
Set<Artifact> comparisonArtifacts = resolveArtifact(dependency, mavenParameters, false, pluginParameters, ConfigurationVersion.OLD);
if (!comparisonArtifacts.isEmpty()) {
setUpClassPathEntries(comparisonArtifacts.iterator().next(), mavenParameters, pluginParameters, comparatorOptions.getOldClassPath());
}
}
} else if (pluginParameters.getOldVersionsParam() != null) {
// TODO
} else {
// TODO rather than calling getComparisonArtifact again, populateArchivesListsFromParameters should compute the old CP for various modes
Artifact comparisonArtifact = getComparisonArtifact(mavenParameters, pluginParameters, ConfigurationVersion.OLD);
if (comparisonArtifact.getVersion() != null) {
setUpClassPathEntries(comparisonArtifact, mavenParameters, pluginParameters, comparatorOptions.getOldClassPath());
}
}
}
if (pluginParameters.getNewVersionParam() != null) {
Dependency dependency = pluginParameters.getNewVersionParam().getDependency();
if (dependency != null) {
Set<Artifact> comparisonArtifacts = resolveArtifact(dependency, mavenParameters, false, pluginParameters, ConfigurationVersion.NEW);
if (!comparisonArtifacts.isEmpty()) {
setUpClassPathEntries(comparisonArtifacts.iterator().next(), mavenParameters, pluginParameters, comparatorOptions.getNewClassPath());
}
}
} else if (pluginParameters.getNewVersionsParam() != null) {
// TODO
} else {
MavenProject mavenProject = mavenParameters.getMavenProject();
notNull(mavenProject, "Maven parameter mavenProject should be provided by maven container.");
String packaging = mavenProject.getPackaging();
packaging = mapPackaging(packaging, "bundle".equalsIgnoreCase(packaging), "jar");
DefaultArtifact artifact = new DefaultArtifact(mavenProject.getGroupId(), mavenProject.getArtifactId(), packaging, mavenProject.getVersion());
setUpClassPathEntries(artifact, mavenParameters, pluginParameters, comparatorOptions.getClassPathEntries());
}
}

private void setUpClassPathEntries(Artifact comparisonArtifact, MavenParameters mavenParameters, PluginParameters pluginParameters, List<String> classPath) throws MojoFailureException {
getLog().debug("setting up classpath entries for " + comparisonArtifact);
CollectRequest request = new CollectRequest();
String packaging = mavenProject.getPackaging();
packaging = mapPackaging(packaging, "bundle".equalsIgnoreCase(packaging), "jar");
DefaultArtifact defaultArtifact = new DefaultArtifact(mavenProject.getGroupId(), mavenProject.getArtifactId(), packaging, mavenProject.getVersion());
request.setRoot(new org.eclipse.aether.graph.Dependency(defaultArtifact, "compile"));
request.setRoot(new org.eclipse.aether.graph.Dependency(comparisonArtifact, "compile"));
try {
DependencyResult dependencyResult = mavenParameters.getRepoSystem().resolveDependencies(mavenParameters.getRepoSession(), new DependencyRequest(
request, new DependencyFilter() {
Expand All @@ -678,7 +719,7 @@ public boolean accept(final DependencyNode node, final List<DependencyNode> pare
}
}
for (String classPathEntry : classPathEntries) {
comparatorOptions.getClassPathEntries().add(classPathEntry);
classPath.add(classPathEntry);
}
} catch (final DependencyResolutionException e) {
throw new MojoFailureException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v1

public class InterfaceMethodAddedImpl implements InterfaceMethodAdded {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v1

public class InterfaceMethodRemovedImpl implements InterfaceMethodRemoved {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v1

public class SubclassAddsNewStaticField extends SuperclassWithSubclassAddsNewStaticField {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package japicmp.test.service; // v1

public class SuperclassChanged extends SuperclassV1 {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v2

public class InterfaceMethodAddedImpl implements InterfaceMethodAdded {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v2

public class InterfaceMethodRemovedImpl implements InterfaceMethodRemoved {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v2

public class SubclassAddsNewStaticField extends SuperclassWithSubclassAddsNewStaticField {
public static int field = 42;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package japicmp.test.service; // v2

public class SuperclassChanged extends SuperclassV2 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,6 @@
<version>${project.version}</version>
</dependency>
</newVersion>
<oldClassPathDependencies>
<dependency>
<groupId>com.github.siom79.japicmp</groupId>
<artifactId>japicmp-test-service-v1</artifactId>
<version>${project.version}</version>
</dependency>
</oldClassPathDependencies>
<newClassPathDependencies>
<dependency>
<groupId>com.github.siom79.japicmp</groupId>
<artifactId>japicmp-test-service-v2</artifactId>
<version>${project.version}</version>
</dependency>
</newClassPathDependencies>
<parameter>
<accessModifier>public</accessModifier>
<onlyModified>true</onlyModified>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public void testTextOutput() throws IOException {
Optional<String> lineOptional = findLineThatContains(lines, "SubclassAddsNewStaticField");
assertThat(lineOptional.isPresent(), is(true));
assertThat(lineOptional.get().contains("***!"), is(true));
lineOptional = findLineThatContains(lines, "SuperclassChanged");
assertThat(lineOptional.isPresent(), is(true));
assertThat(lineOptional.get().contains("***!"), is(true));
}

private Optional<String> findLineThatContains(List<String> lines, String str) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v1

public interface InterfaceMethodAdded {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v1

public interface InterfaceMethodRemoved {
void methodRemoved();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package japicmp.test.service; // v1

public class SuperclassV1 {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v1

public class SuperclassWithSubclassAddsNewStaticField {
public static final int field = 42;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v2

public interface InterfaceMethodAdded {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v2

public interface InterfaceMethodRemoved {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package japicmp.test.service; // v2

public class SuperclassV2 {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package japicmp.test.service;
package japicmp.test.service; // v2

public class SuperclassWithSubclassAddsNewStaticField {
public static final int field = 42;
Expand Down
6 changes: 6 additions & 0 deletions japicmp/src/main/java/japicmp/cmp/JApiCmpArchive.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public File getFile() {
public Version getVersion() {
return version;
}

@Override
public String toString() {
return file + "@" + version.getStringVersion();
}

}