Skip to content

Commit f0efd60

Browse files
authored
Respect CompilerConfiguration.sourceFiles in EclipseJavaCompiler (#233)
Just like JavacCompiler does, to avoid "FilerException: Source file already created" during annotation processing when running mvn without clean after changing code.
1 parent e136e4c commit f0efd60

File tree

9 files changed

+269
-51
lines changed

9 files changed

+269
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.java.version = 1.8+
19+
20+
# without-dummy profile is used to have compiler plugin do actual compilation in the second run
21+
invoker.name.1 = Initial build
22+
invoker.goals.1 = clean compile -Pwithout-dummy
23+
invoker.buildResult.1 = success
24+
25+
invoker.name.2 = Subsequent build without clean
26+
invoker.goals.2 = compile
27+
invoker.buildResult.2 = success
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.codehaus.plexus.compiler.it</groupId>
27+
<artifactId>simple-javac</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
30+
<name>Test for default configuration</name>
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
<maven.compiler.target>1.8</maven.compiler.target>
37+
38+
<org.mapstruct.version>1.5.2.Final</org.mapstruct.version>
39+
</properties>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>org.mapstruct</groupId>
44+
<artifactId>mapstruct</artifactId>
45+
<version>${org.mapstruct.version}</version>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-compiler-plugin</artifactId>
54+
<version>3.10.1</version>
55+
<configuration>
56+
<compilerId>eclipse</compilerId>
57+
</configuration>
58+
<dependencies>
59+
<dependency>
60+
<groupId>org.codehaus.plexus</groupId>
61+
<artifactId>plexus-compiler-api</artifactId>
62+
<version>@pom.version@</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.codehaus.plexus</groupId>
66+
<artifactId>plexus-compiler-eclipse</artifactId>
67+
<version>@pom.version@</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.mapstruct</groupId>
71+
<artifactId>mapstruct-processor</artifactId>
72+
<version>${org.mapstruct.version}</version>
73+
</dependency>
74+
</dependencies>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
79+
<profiles>
80+
<profile>
81+
<id>without-dummy</id>
82+
<build>
83+
<plugins>
84+
<plugin>
85+
<groupId>org.apache.maven.plugins</groupId>
86+
<artifactId>maven-compiler-plugin</artifactId>
87+
<configuration>
88+
<excludes>
89+
<exclude>**/Dummy.java</exclude>
90+
</excludes>
91+
</configuration>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
</profile>
96+
</profiles>
97+
98+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
public class Car
21+
{
22+
public String make;
23+
public int numberOfSeats;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
public class CarDto
21+
{
22+
public String make;
23+
public int seatCount;
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import org.mapstruct.Mapper;
21+
import org.mapstruct.Mapping;
22+
import org.mapstruct.factory.Mappers;
23+
24+
@Mapper
25+
public interface CarMapper
26+
{
27+
28+
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
29+
30+
@Mapping( source = "numberOfSeats", target = "seatCount" )
31+
CarDto carToCarDto( Car car );
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
public class Dummy
21+
{
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
def mapperImplClass = new File( basedir, "target/classes/CarMapperImpl.class" )
20+
assert mapperImplClass.exists()
21+
22+
def dummyClass = new File( basedir, "target/classes/Dummy.class" )
23+
assert dummyClass.exists()
24+
25+
File buildLog = new File( basedir, 'build.log' )
26+
assert buildLog.text.count( "Changes detected - recompiling the module!" ) == 2

plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java

+4-39
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,12 @@
3737
import java.nio.charset.IllegalCharsetNameException;
3838
import java.nio.charset.UnsupportedCharsetException;
3939
import java.util.ArrayList;
40-
import java.util.HashSet;
40+
import java.util.Arrays;
4141
import java.util.Iterator;
42-
import java.util.LinkedHashSet;
4342
import java.util.List;
4443
import java.util.Locale;
4544
import java.util.Map.Entry;
4645
import java.util.ServiceLoader;
47-
import java.util.Set;
48-
4946
import org.codehaus.plexus.compiler.AbstractCompiler;
5047
import org.codehaus.plexus.compiler.Compiler;
5148
import org.codehaus.plexus.compiler.CompilerConfiguration;
@@ -54,7 +51,6 @@
5451
import org.codehaus.plexus.compiler.CompilerOutputStyle;
5552
import org.codehaus.plexus.compiler.CompilerResult;
5653
import org.codehaus.plexus.component.annotations.Component;
57-
import org.codehaus.plexus.util.DirectoryScanner;
5854
import org.codehaus.plexus.util.StringUtils;
5955
import org.eclipse.jdt.core.compiler.CompilationProgress;
6056
import org.eclipse.jdt.core.compiler.batch.BatchCompiler;
@@ -169,14 +165,12 @@ public CompilerResult performCompile( CompilerConfiguration config )
169165
args.add( config.getOutputLocation() );
170166

171167
// Annotation processors defined?
172-
List<String> extraSourceDirs = new ArrayList<>();
173168
if ( !isPreJava1_6( config ) )
174169
{
175170
File generatedSourcesDir = config.getGeneratedSourcesDirectory();
176171
if ( generatedSourcesDir != null )
177172
{
178173
generatedSourcesDir.mkdirs();
179-
extraSourceDirs.add( generatedSourcesDir.getAbsolutePath() );
180174

181175
//-- option to specify where annotation processor is to generate its output
182176
args.add( "-s" );
@@ -247,24 +241,7 @@ public CompilerResult performCompile( CompilerConfiguration config )
247241
}
248242

249243
// Collect sources
250-
Set<String> allSources = new HashSet<>();
251-
for ( String source : config.getSourceLocations() )
252-
{
253-
File srcFile = new File( source );
254-
if ( srcFile.exists() )
255-
{
256-
Set<String> ss = getSourceFilesForSourceRoot( config, source );
257-
allSources.addAll( ss );
258-
}
259-
}
260-
for ( String extraSrcDir : extraSourceDirs )
261-
{
262-
File extraDir = new File( extraSrcDir );
263-
if ( extraDir.isDirectory() )
264-
{
265-
addExtraSources( extraDir, allSources );
266-
}
267-
}
244+
List<String> allSources = Arrays.asList( getSourceFiles( config ) );
268245
List<CompilerMessage> messageList = new ArrayList<>();
269246
if ( allSources.isEmpty() )
270247
{
@@ -504,9 +481,9 @@ static boolean isReplaceProcessorPath( CompilerConfiguration config )
504481
return false;
505482
}
506483

507-
static Set<String> resortSourcesToPutModuleInfoFirst( Set<String> allSources )
484+
static List<String> resortSourcesToPutModuleInfoFirst( List<String> allSources )
508485
{
509-
Set<String> resorted = new LinkedHashSet<>( allSources.size() );
486+
List<String> resorted = new ArrayList<>( allSources.size() );
510487

511488
for ( String mi : allSources )
512489
{
@@ -654,18 +631,6 @@ private JavaCompiler getEcj()
654631
return null;
655632
}
656633

657-
private void addExtraSources( File dir, Set<String> allSources )
658-
{
659-
DirectoryScanner scanner = new DirectoryScanner();
660-
scanner.setBasedir( dir.getAbsolutePath() );
661-
scanner.setIncludes( new String[]{ "**/*.java" } );
662-
scanner.scan();
663-
for ( String file : scanner.getIncludedFiles() )
664-
{
665-
allSources.add( new File( dir, file ).getAbsolutePath() );
666-
}
667-
}
668-
669634
private CompilerMessage.Kind convert( Diagnostic.Kind kind )
670635
{
671636
if ( kind == null )

0 commit comments

Comments
 (0)