Skip to content

Commit

Permalink
Consider classpath dependencies in "build needed" check
Browse files Browse the repository at this point in the history
  • Loading branch information
kriegaex committed Sep 19, 2023
1 parent 903def2 commit 7b8706a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/org/codehaus/mojo/aspectj/AbstractAjcCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -882,6 +883,31 @@ private boolean hasNonWeavedClassesChanged(File outDir)
return false;
}

protected boolean hasClassPathClassesChanged(File outDir, boolean isTestCompile)
throws MojoExecutionException
{
List<String> classpathDirectories;
try {
classpathDirectories = isTestCompile
? project.getTestClasspathElements()
: project.getCompileClasspathElements();
}
catch (DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Cannot determine compile classpath elements", e);
}
if (classpathDirectories != null) {
Set<String> weaveSources = AjcHelper.getClassFilesAndJars(classpathDirectories.toArray(new String[0]), outDir);
long lastBuild = new File(outDir, argumentFileName).lastModified();
for (String source : weaveSources) {
File sourceFile = new File(source);
long sourceModified = sourceFile.lastModified();
if (sourceModified >= lastBuild)
return true;
}
}
return false;
}

public void setDeprecation(boolean deprecation) {
if (deprecation) {
ajcOptions.add("-deprecation");
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/codehaus/mojo/aspectj/AjcCompileMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collections;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -130,4 +131,9 @@ protected String getAdditionalAspectPaths()
{
return null;
}

@Override
protected boolean isBuildNeeded() throws MojoExecutionException {
return super.isBuildNeeded() || hasClassPathClassesChanged(getArgumentFileDirectory(), false);
}
}
34 changes: 34 additions & 0 deletions src/main/java/org/codehaus/mojo/aspectj/AjcHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,40 @@ public static Set<String> getWeaveSourceFiles( String[] weaveDirs )
return result;
}

/**
* Based on a set of JARs and directories, returns a set of all JARs and class files
*
* @param dirsAndJars JARs and base directories to be scanned
* @param outDir
* @return a set of all JARs and class files found in the <i>dirsAndJars</i>
* @throws MojoExecutionException if any IOExceptions occur
*/
public static Set<String> getClassFilesAndJars(String[] dirsAndJars, File outDir)
throws MojoExecutionException
{
Set<String> result = new HashSet<>();
for (int i = 0; i < dirsAndJars.length; i++) {
String currentDir = dirsAndJars[i];
if (FileUtils.fileExists(currentDir)) {
if (outDir.equals(new File(currentDir)))
continue;
if (new File(currentDir).isDirectory()) {
try {
result.addAll(FileUtils.getFileNames(new File(currentDir), "**/*.class", DEFAULT_EXCLUDES, true));
}
catch (IOException e) {
throw new MojoExecutionException("IO Error resolving directories", e);
}
}
else {
if (currentDir.toLowerCase().endsWith(".jar"))
result.add(currentDir);
}
}
}
return result;
}

/**
* Creates a file that can be used as input to the ajc compiler using the -argdfile flag.
* Each line in these files should contain one option or filename.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,9 @@ private boolean isSkipTestCompile()
String skipTestCompile = System.getProperty(MAVEN_TEST_SKIP);
return Boolean.parseBoolean(skipTestCompile);
}

@Override
protected boolean isBuildNeeded() throws MojoExecutionException {
return super.isBuildNeeded() || hasClassPathClassesChanged(getArgumentFileDirectory(), true);
}
}

0 comments on commit 7b8706a

Please sign in to comment.