diff --git a/plexus-compiler-its/pom.xml b/plexus-compiler-its/pom.xml index 2d9f97cf..c5979a64 100644 --- a/plexus-compiler-its/pom.xml +++ b/plexus-compiler-its/pom.xml @@ -39,7 +39,8 @@ verify - true + false + true src/main/it ${project.build.directory}/it verify diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/invoker.properties b/plexus-compiler-its/src/main/it/aspectj-compiler/invoker.properties new file mode 100644 index 00000000..788d7f9f --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/invoker.properties @@ -0,0 +1,20 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.java.version = 1.8+ +invoker.goals = clean test +#invoker.buildResult = failure diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/pom.xml b/plexus-compiler-its/src/main/it/aspectj-compiler/pom.xml new file mode 100644 index 00000000..8c7ab602 --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/pom.xml @@ -0,0 +1,73 @@ + + + + + 4.0.0 + + org.apache.maven.plugins.compiler.it + aspectj-compiler + 1.0-SNAPSHOT + + + UTF-8 + UTF-8 + 1.8 + 1.8 + + + + + + maven-compiler-plugin + 3.8.1 + + aspectj + + + + org.codehaus.plexus + plexus-compiler-api + @pom.version@ + + + org.codehaus.plexus + plexus-compiler-aspectj + @pom.version@ + + + + + + + + + junit + junit + @junit.version@ + test + + + org.aspectj + aspectjrt + @aspectj.version@ + + + diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/Application.java b/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/Application.java new file mode 100644 index 00000000..dbd86b18 --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/Application.java @@ -0,0 +1,12 @@ +package org.acme; + +public class Application { + public static void main(String[] args) { + System.out.println("Running application"); + new Application().greet(args[0]); + } + + public String greet(String name) { + return "Hello " + name; + } +} diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/MyAnnotationDrivenAspect.aj b/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/MyAnnotationDrivenAspect.aj new file mode 100644 index 00000000..1cebb9c7 --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/MyAnnotationDrivenAspect.aj @@ -0,0 +1,13 @@ +package org.acme; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; + +@Aspect +public class MyAnnotationDrivenAspect { + @Before("execution(static * *(..)) && within(Application)") + public void myAdvice(JoinPoint joinPoint) { + System.out.println(joinPoint); + } +} diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/MyNativeAspect.aj b/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/MyNativeAspect.aj new file mode 100644 index 00000000..b70efcf6 --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/MyNativeAspect.aj @@ -0,0 +1,7 @@ +package org.acme; + +public aspect MyNativeAspect { + before() : execution(!static * *(..)) && within(Application) { + System.out.println(thisJoinPoint); + } +} diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/do-not-try-to-compile.txt b/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/do-not-try-to-compile.txt new file mode 100644 index 00000000..7d0fddb6 --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/src/main/java/org/acme/do-not-try-to-compile.txt @@ -0,0 +1,7 @@ +The Apache Maven Compiler (MC) API only allows a single source file extension, such as '.java'. +The AspectJ Compiler (AJC) should consider two default extensions, though: '.java' and '.aj'. +In order to achieve that, the Plexus AJC component tells MC to give it all files, +subsequently filtering for those two extensions. + +The purpose of this file is to make sure that even though MC finds it in the source folder, +Plexus AJC filters it out and AJC does not try to compile it. diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/src/test/java/org/acme/ApplicationTest.java b/plexus-compiler-its/src/main/it/aspectj-compiler/src/test/java/org/acme/ApplicationTest.java new file mode 100644 index 00000000..9e2dec86 --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/src/test/java/org/acme/ApplicationTest.java @@ -0,0 +1,19 @@ +package org.acme; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class ApplicationTest { + @Test + public void testGreet() { + assertEquals("Hello Jane", new Application().greet("Jane")); + } + + @Test + public void testMain() { + Application.main(new String[] { "Joe" }); + assertTrue(true); + } +} diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/src/test/java/org/acme/TestAspect.java b/plexus-compiler-its/src/main/it/aspectj-compiler/src/test/java/org/acme/TestAspect.java new file mode 100644 index 00000000..da89cacb --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/src/test/java/org/acme/TestAspect.java @@ -0,0 +1,13 @@ +package org.acme; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; + +@Aspect +public class TestAspect { + @Before("call(* *(..)) && !within(TestAspect)") + public void beforeCall(JoinPoint joinPoint) { + System.out.println(joinPoint); + } +} diff --git a/plexus-compiler-its/src/main/it/aspectj-compiler/verify.groovy b/plexus-compiler-its/src/main/it/aspectj-compiler/verify.groovy new file mode 100644 index 00000000..d1c4410d --- /dev/null +++ b/plexus-compiler-its/src/main/it/aspectj-compiler/verify.groovy @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +def logFile = new File( basedir, 'build.log' ) +assert logFile.exists() +content = logFile.text.normalize() + +def junitLog = """Running org.acme.ApplicationTest +call(String org.acme.Application.greet(String)) +execution(String org.acme.Application.greet(String)) +call(void org.junit.Assert.assertEquals(Object, Object)) +call(void org.acme.Application.main(String[])) +execution(void org.acme.Application.main(String[])) +Running application +execution(String org.acme.Application.greet(String)) +call(void org.junit.Assert.assertTrue(boolean)) +Tests run: 2, Failures: 0, Errors: 0, Skipped: 0""".normalize() + +assert content.contains( junitLog ) diff --git a/plexus-compiler-its/src/main/it/error-prone-compiler/pom.xml b/plexus-compiler-its/src/main/it/error-prone-compiler/pom.xml index d3b0b37b..2722f0a9 100644 --- a/plexus-compiler-its/src/main/it/error-prone-compiler/pom.xml +++ b/plexus-compiler-its/src/main/it/error-prone-compiler/pom.xml @@ -38,7 +38,8 @@ junit junit - 3.8.2 + @junit.version@ + test diff --git a/plexus-compiler-its/src/main/it/simple-javac/pom.xml b/plexus-compiler-its/src/main/it/simple-javac/pom.xml index 6bb02053..331d5b7d 100644 --- a/plexus-compiler-its/src/main/it/simple-javac/pom.xml +++ b/plexus-compiler-its/src/main/it/simple-javac/pom.xml @@ -37,7 +37,8 @@ junit junit - 3.8.2 + @junit.version@ + test diff --git a/plexus-compilers/plexus-compiler-aspectj/pom.xml b/plexus-compilers/plexus-compiler-aspectj/pom.xml index f129dffc..74762538 100644 --- a/plexus-compilers/plexus-compiler-aspectj/pom.xml +++ b/plexus-compilers/plexus-compiler-aspectj/pom.xml @@ -13,10 +13,6 @@ Plexus AspectJ Compiler AspectJ Compiler support for Plexus Compiler component. - - 1.9.6 - - org.codehaus.plexus diff --git a/plexus-compilers/plexus-compiler-aspectj/src/main/java/org/codehaus/plexus/compiler/ajc/AspectJCompiler.java b/plexus-compilers/plexus-compiler-aspectj/src/main/java/org/codehaus/plexus/compiler/ajc/AspectJCompiler.java index 286407cd..0f70efca 100644 --- a/plexus-compilers/plexus-compiler-aspectj/src/main/java/org/codehaus/plexus/compiler/ajc/AspectJCompiler.java +++ b/plexus-compilers/plexus-compiler-aspectj/src/main/java/org/codehaus/plexus/compiler/ajc/AspectJCompiler.java @@ -17,6 +17,7 @@ import org.codehaus.plexus.compiler.CompilerOutputStyle; import org.codehaus.plexus.compiler.CompilerResult; import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.util.DirectoryScanner; import java.io.File; import java.io.IOException; @@ -25,9 +26,11 @@ import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; /** *

@@ -295,7 +298,9 @@ public class AspectJCompiler public AspectJCompiler() { - super( CompilerOutputStyle.ONE_OUTPUT_FILE_PER_INPUT_FILE, ".java", ".class", null ); + // Input file ending "" means: Give me all files, I am going to filter them myself later. We are doing this, + // because in method 'getSourceFiles' we need to search for both ".java" and ".aj" files. + super( CompilerOutputStyle.ONE_OUTPUT_FILE_PER_INPUT_FILE, "", ".class", null ); } public CompilerResult performCompile( CompilerConfiguration config ) @@ -344,15 +349,17 @@ private AjBuildConfig buildCompilerConfig( CompilerConfiguration config ) buildConfig.setFiles( buildFileList( Arrays.asList( files ) ) ); } - setSourceVersion( buildConfig, config.getSourceVersion() ); + String releaseVersion = config.getReleaseVersion(); + setSourceVersion( buildConfig, releaseVersion == null ? config.getSourceVersion() : releaseVersion ); + setTargetVersion( buildConfig, releaseVersion == null ? config.getTargetVersion() : releaseVersion ); if ( config.isDebug() ) { buildConfig.getOptions().produceDebugAttributes = - ClassFileConstants.ATTR_SOURCE + ClassFileConstants.ATTR_LINES + ClassFileConstants.ATTR_VARS; + ClassFileConstants.ATTR_SOURCE + ClassFileConstants.ATTR_LINES + ClassFileConstants.ATTR_VARS; } - Map javaOpts = config.getCustomCompilerArguments(); + Map javaOpts = config.getCustomCompilerArgumentsAsMap(); if ( javaOpts != null && !javaOpts.isEmpty() ) { // TODO support customCompilerArguments @@ -557,7 +564,7 @@ private List buildFileList( List locations ) } /** - * Set the source version in ajc compiler + * Set the source version in AspectJ compiler * * @param buildConfig * @param sourceVersion @@ -565,75 +572,149 @@ private List buildFileList( List locations ) private void setSourceVersion( AjBuildConfig buildConfig, String sourceVersion ) throws CompilerException { - if ( "11".equals( sourceVersion ) ) - { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK11; - } - else if ( "10".equals( sourceVersion ) ) - { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK10; - } - else if ( "9".equals( sourceVersion ) ) - { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK9; - } - else if ( "1.9".equals( sourceVersion ) ) - { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK9; - } - else if ( "1.8".equals( sourceVersion ) ) - { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_8; - } - else if ( "1.7".equals( sourceVersion ) ) - { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_7; - } - else if ( "1.6".equals( sourceVersion ) ) - { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_6; - } - else if ( "1.5".equals( sourceVersion ) ) + buildConfig.getOptions().sourceLevel = versionStringToMajorMinor( sourceVersion ); + } + + /** + * Set the target version in AspectJ compiler + * + * @param buildConfig + * @param targetVersion + */ + private void setTargetVersion( AjBuildConfig buildConfig, String targetVersion ) + throws CompilerException + { + buildConfig.getOptions().targetJDK = versionStringToMajorMinor( targetVersion ); + } + + private static long versionStringToMajorMinor(String version) throws CompilerException + { + if ( version == null ) + { + version = ""; + } + // Note: We avoid using org.codehaus.plexus:plexus-java here on purpose, because Maven Compiler might depend on + // a different (older) versionm, e.g. not having the 'asMajor' method yet. This can cause problems for users + // trying to compile their AspectJ code using Plexus. + + version = version.trim() + // Cut off leading "1.", focusing on the Java major + .replaceFirst( "^1[.]", "" ) + // Accept, but cut off trailing ".0", as ECJ/ACJ explicitly support versions like 5.0, 8.0, 11.0 + .replaceFirst("[.]0$", ""); + + switch ( version ) + { + // Java 1.6 as a default source/target seems to make sense. Maven Compiler should set its own default + // anyway, so this probably never needs to be used. But not having a default feels bad, too. + case "" : return ClassFileConstants.JDK1_6; + case "1" : return ClassFileConstants.JDK1_1; + case "2" : return ClassFileConstants.JDK1_2; + case "3" : return ClassFileConstants.JDK1_3; + case "4" : return ClassFileConstants.JDK1_4; + case "5" : return ClassFileConstants.JDK1_5; + case "6" : return ClassFileConstants.JDK1_6; + case "7" : return ClassFileConstants.JDK1_7; + case "8" : return ClassFileConstants.JDK1_8; + case "9" : return ClassFileConstants.JDK9; + case "10" : return ClassFileConstants.JDK10; + case "11" : return ClassFileConstants.JDK11; + case "12" : return ClassFileConstants.JDK12; + case "13" : return ClassFileConstants.JDK13; + case "14" : return ClassFileConstants.JDK14; + case "15" : return ClassFileConstants.JDK15; + case "16" : return ClassFileConstants.JDK16; + } + throw new CompilerException( "Unknown Java source/target version number: " + version ); + } + + /** + * @return null + */ + public String[] createCommandLine( CompilerConfiguration config ) + throws CompilerException + { + return null; + } + + protected static String[] getSourceFiles( CompilerConfiguration config ) + { + Set sources = new HashSet<>(); + + Set sourceFiles = config.getSourceFiles(); + + if ( sourceFiles != null && !sourceFiles.isEmpty() ) { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_5; + for ( File sourceFile : sourceFiles ) + { + if ( sourceFile.getName().endsWith( ".java" ) || sourceFile.getName().endsWith( ".aj" ) ) + { + sources.add( sourceFile.getAbsolutePath() ); + } + } } - else if ( "5.0".equals( sourceVersion ) ) + else { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_5; + for ( String sourceLocation : config.getSourceLocations() ) + { + sources.addAll( getSourceFilesForSourceRoot( config, sourceLocation ) ); + } } - else if ( "1.4".equals( sourceVersion ) ) + + String[] result; + + if ( sources.isEmpty() ) { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_4; + result = new String[0]; } - else if ( "1.3".equals( sourceVersion ) ) + else { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_3; + result = sources.toArray( new String[sources.size()] ); } - else if ( "1.2".equals( sourceVersion ) ) + + return result; + } + + protected static Set getSourceFilesForSourceRoot( CompilerConfiguration config, String sourceLocation ) + { + DirectoryScanner scanner = new DirectoryScanner(); + + scanner.setBasedir( sourceLocation ); + + Set includes = config.getIncludes(); + + if ( includes != null && !includes.isEmpty() ) { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_2; + String[] inclStrs = includes.toArray( new String[includes.size()] ); + scanner.setIncludes( inclStrs ); } - else if ( "1.1".equals( sourceVersion ) ) + else { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_1; + scanner.setIncludes( new String[] {"**/*.java", "**/*.aj"} ); } - else if ( sourceVersion == null || sourceVersion.length() <= 0 ) + + Set excludes = config.getExcludes(); + + if ( excludes != null && !excludes.isEmpty() ) { - buildConfig.getOptions().sourceLevel = ClassFileConstants.JDK1_3; + String[] exclStrs = excludes.toArray( new String[excludes.size()] ); + scanner.setExcludes( exclStrs ); } - else + + scanner.scan(); + + String[] sourceDirectorySources = scanner.getIncludedFiles(); + + Set sources = new HashSet<>(); + + for ( String sourceDirectorySource : sourceDirectorySources ) { - throw new CompilerException( "The source version was not recognized: " + sourceVersion ); + File f = new File( sourceLocation, sourceDirectorySource ); + + sources.add( f.getPath() ); } - } - /** - * @return null - */ - public String[] createCommandLine( CompilerConfiguration config ) - throws CompilerException - { - return null; + return sources; } } diff --git a/plexus-compilers/plexus-compiler-aspectj/src/test/java/org/codehaus/plexus/compiler/ajc/AspectJCompilerTest.java b/plexus-compilers/plexus-compiler-aspectj/src/test/java/org/codehaus/plexus/compiler/ajc/AspectJCompilerTest.java index e3c9835b..fceb9277 100644 --- a/plexus-compilers/plexus-compiler-aspectj/src/test/java/org/codehaus/plexus/compiler/ajc/AspectJCompilerTest.java +++ b/plexus-compilers/plexus-compiler-aspectj/src/test/java/org/codehaus/plexus/compiler/ajc/AspectJCompilerTest.java @@ -3,7 +3,6 @@ import java.io.File; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; import org.codehaus.plexus.compiler.AbstractCompilerTest; @@ -24,40 +23,19 @@ protected String getRoleHint() return "aspectj"; } - protected int expectedErrors() - { - // olamy well I agree it's hackhish but I don't want to waste too much time with aspectj which is probably - // not used a lot anymore... - String javaVersion = getJavaVersion(); - if (javaVersion.equals( "11" )) - { - return 11; - } - return 1; - } - protected Collection expectedOutputFiles() { - String javaVersion = System.getProperty( "java.version" ); - // olamy well I agree it's hackhish but I don't want to waste too much time with aspectj which is probably - // not used a lot anymore... -// if (javaVersion.startsWith( "9" ) || javaVersion.startsWith( "10" )) -// { -// return Collections.emptyList(); -// } - return Arrays.asList( new String[]{ "org/codehaus/foo/ExternalDeps.class", "org/codehaus/foo/Person.class" } ); + return Arrays.asList( "org/codehaus/foo/ExternalDeps.class", "org/codehaus/foo/Person.class" ); } protected List getClasspath() throws Exception { - List cp = super.getClasspath(); - - File localArtifactPath = - getLocalArtifactPath( "org.aspectj", "aspectjrt", System.getProperty( "aspectj.version" ), "jar" ); - cp.add( localArtifactPath.getAbsolutePath() ); - - return cp; + List classpath = super.getClasspath(); + String aspectjVersion = System.getProperty( "aspectj.version" ); + File aspectjRuntime = getLocalArtifactPath( "org.aspectj", "aspectjrt", aspectjVersion, "jar" ); + classpath.add( aspectjRuntime.getAbsolutePath() ); + return classpath; } } diff --git a/pom.xml b/pom.xml index 3605183c..c35d8a2c 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,8 @@ 7 true 2020-08-24T00:30:49Z + 4.13.2 + 1.9.7.M3 2.6.0 @@ -80,7 +82,7 @@ junit junit test - 4.13.2 + ${junit.version}