Skip to content

Commit 45a8d29

Browse files
gnodetmichael-o
authored andcommitted
[MJAVADOC-716] Fix stale files detection failing because of the added newline at the end of the file
This closes #144
1 parent afb2dee commit 45a8d29

File tree

5 files changed

+228
-8
lines changed

5 files changed

+228
-8
lines changed

src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -5083,11 +5083,11 @@ private void executeJavadocCommandLine(Commandline cmd, File javadocOutputDirect
50835083
*/
50845084
private boolean isUpToDate(Commandline cmd) throws MavenReportException {
50855085
try {
5086-
String curdata = StaleHelper.getStaleData(cmd);
5086+
List<String> curdata = StaleHelper.getStaleData(cmd);
50875087
Path cacheData = staleDataPath.toPath();
5088-
String prvdata;
5088+
List<String> prvdata;
50895089
if (Files.isRegularFile(cacheData)) {
5090-
prvdata = new String(Files.readAllBytes(cacheData), StandardCharsets.UTF_8);
5090+
prvdata = Files.lines(cacheData, StandardCharsets.UTF_8).collect(Collectors.toList());
50915091
} else {
50925092
prvdata = null;
50935093
}
@@ -5099,6 +5099,18 @@ private boolean isUpToDate(Commandline cmd) throws MavenReportException {
50995099
getLog().info("No previous run data found, generating javadoc.");
51005100
} else {
51015101
getLog().info("Configuration changed, re-generating javadoc.");
5102+
if (getLog().isDebugEnabled()) {
5103+
List<String> newStrings = new ArrayList<>(curdata);
5104+
List<String> remStrings = new ArrayList<>(prvdata);
5105+
newStrings.removeAll(prvdata);
5106+
remStrings.removeAll(curdata);
5107+
if (!remStrings.isEmpty()) {
5108+
getLog().debug(" Removed: " + String.join(", ", remStrings));
5109+
}
5110+
if (!newStrings.isEmpty()) {
5111+
getLog().debug(" Added: " + String.join(", ", newStrings));
5112+
}
5113+
}
51025114
}
51035115
}
51045116
} catch (IOException e) {

src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
import org.apache.maven.reporting.MavenReportException;
3434
import org.codehaus.plexus.languages.java.version.JavaVersion;
35-
import org.codehaus.plexus.util.StringUtils;
3635
import org.codehaus.plexus.util.cli.Commandline;
3736

3837
/**
@@ -48,7 +47,7 @@ public class StaleHelper {
4847
* @return the stale data
4948
* @throws MavenReportException if an error occurs
5049
*/
51-
public static String getStaleData(Commandline cmd) throws MavenReportException {
50+
public static List<String> getStaleData(Commandline cmd) throws MavenReportException {
5251
try {
5352
List<String> ignored = new ArrayList<>();
5453
List<String> options = new ArrayList<>();
@@ -101,7 +100,7 @@ public static String getStaleData(Commandline cmd) throws MavenReportException {
101100
state.add(p + " = " + lastmod(p));
102101
}
103102
}
104-
return StringUtils.join(state.iterator(), SystemUtils.LINE_SEPARATOR);
103+
return state;
105104
} catch (Exception e) {
106105
throw new MavenReportException("Unable to compute stale date", e);
107106
}
@@ -116,9 +115,9 @@ public static String getStaleData(Commandline cmd) throws MavenReportException {
116115
*/
117116
public static void writeStaleData(Commandline cmd, Path path) throws MavenReportException {
118117
try {
119-
String curdata = getStaleData(cmd);
118+
List<String> curdata = getStaleData(cmd);
120119
Files.createDirectories(path.getParent());
121-
Files.write(path, Collections.singleton(curdata), Charset.defaultCharset());
120+
Files.write(path, curdata, StandardCharsets.UTF_8);
122121
} catch (IOException e) {
123122
throw new MavenReportException("Error checking stale data", e);
124123
}

src/test/java/org/apache/maven/plugins/javadoc/JavadocJarTest.java

+115
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919
package org.apache.maven.plugins.javadoc;
2020

2121
import java.io.File;
22+
import java.util.ArrayList;
2223
import java.util.Enumeration;
2324
import java.util.HashSet;
25+
import java.util.List;
2426
import java.util.Set;
2527
import java.util.zip.ZipEntry;
2628
import java.util.zip.ZipFile;
2729

2830
import org.apache.maven.execution.MavenSession;
2931
import org.apache.maven.model.Plugin;
3032
import org.apache.maven.plugin.MojoExecution;
33+
import org.apache.maven.plugin.logging.Log;
3134
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
3235
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
3336
import org.apache.maven.project.MavenProject;
@@ -191,4 +194,116 @@ public void testIncludeMavenDescriptorWhenExplicitlyConfigured() throws Exceptio
191194
"META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/pom.xml",
192195
"META-INF/maven/org.apache.maven.plugins.maven-javadoc-plugin.unit/javadocjar-archive-config/pom.properties");
193196
}
197+
198+
public void testStale() throws Exception {
199+
File testPom = new File(getBasedir(), "src/test/resources/unit/stale-test/stale-test-plugin-config.xml");
200+
JavadocJar mojo = lookupMojo(testPom);
201+
BufferingLog log = new BufferingLog();
202+
mojo.setLog(log);
203+
204+
Thread.sleep(500);
205+
206+
new File(getBasedir(), "target/test/unit/stale-test/target/maven-javadoc-plugin-stale-data.txt").delete();
207+
mojo.execute();
208+
assertThat(log.getMessages()).contains("[INFO] No previous run data found, generating javadoc.");
209+
210+
Thread.sleep(500);
211+
212+
log.getMessages().clear();
213+
mojo.execute();
214+
assertThat(log.getMessages()).contains("[INFO] Skipping javadoc generation, everything is up to date.");
215+
}
216+
217+
private static class BufferingLog implements Log {
218+
private final List<String> messages = new ArrayList<>();
219+
220+
public List<String> getMessages() {
221+
return messages;
222+
}
223+
224+
@Override
225+
public boolean isDebugEnabled() {
226+
return true;
227+
}
228+
229+
@Override
230+
public void debug(CharSequence charSequence) {
231+
debug(charSequence, null);
232+
}
233+
234+
@Override
235+
public void debug(CharSequence charSequence, Throwable throwable) {
236+
message("DEBUG", charSequence, throwable);
237+
}
238+
239+
@Override
240+
public void debug(Throwable throwable) {
241+
debug(null, throwable);
242+
}
243+
244+
@Override
245+
public boolean isInfoEnabled() {
246+
return true;
247+
}
248+
249+
@Override
250+
public void info(CharSequence charSequence) {
251+
info(charSequence, null);
252+
}
253+
254+
@Override
255+
public void info(CharSequence charSequence, Throwable throwable) {
256+
message("INFO", charSequence, throwable);
257+
}
258+
259+
@Override
260+
public void info(Throwable throwable) {
261+
info(null, throwable);
262+
}
263+
264+
@Override
265+
public boolean isWarnEnabled() {
266+
return true;
267+
}
268+
269+
@Override
270+
public void warn(CharSequence charSequence) {
271+
warn(charSequence, null);
272+
}
273+
274+
@Override
275+
public void warn(CharSequence charSequence, Throwable throwable) {
276+
message("WARN", charSequence, throwable);
277+
}
278+
279+
@Override
280+
public void warn(Throwable throwable) {
281+
warn(null, throwable);
282+
}
283+
284+
@Override
285+
public boolean isErrorEnabled() {
286+
return true;
287+
}
288+
289+
@Override
290+
public void error(CharSequence charSequence) {
291+
error(charSequence, null);
292+
}
293+
294+
@Override
295+
public void error(CharSequence charSequence, Throwable throwable) {
296+
message("ERROR", charSequence, throwable);
297+
}
298+
299+
@Override
300+
public void error(Throwable throwable) {
301+
error(null, throwable);
302+
}
303+
304+
private void message(String level, CharSequence message, Throwable throwable) {
305+
messages.add("[" + level + "]" + (message != null ? " " + message : "")
306+
+ (throwable != null ? " " + throwable : ""));
307+
}
308+
}
194309
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
package maven;
20+
21+
/**
22+
* Hello world!
23+
*
24+
*/
25+
public class App
26+
{
27+
public static void main( String[] args )
28+
{
29+
System.out.println( "Hello World!" );
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<groupId>org.apache.maven.plugins.maven-javadoc-plugin.unit</groupId>
24+
<artifactId>pom-test</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
<packaging>pom</packaging>
27+
<name>project1</name>
28+
<dependencies>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>3.8.1</version>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-javadoc-plugin</artifactId>
41+
<configuration>
42+
<project implementation="org.apache.maven.plugins.javadoc.stubs.JavadocJarArchiveConfigProjectStub"/>
43+
<outputDirectory>${basedir}/target/test/unit/stale-test/target/site/apidocs</outputDirectory>
44+
<javadocOptionsDir>${basedir}/target/test/unit/stale-test/target/javadoc-bundle-options</javadocOptionsDir>
45+
<show>protected</show>
46+
<encoding>ISO-8859-1</encoding>
47+
<groups/>
48+
<tags/>
49+
<windowtitle>Maven Pom 1.0-SNAPSHOT API</windowtitle>
50+
<quiet>true</quiet>
51+
<javadocDirectory>${basedir}/src/test/resources/unit/stale-test/src/java/javadoc</javadocDirectory>
52+
<stylesheet>java</stylesheet>
53+
<debug>true</debug>
54+
<failOnError>true</failOnError>
55+
<reactorProjects>
56+
<project implementation="org.apache.maven.plugins.javadoc.stubs.AggregateNotInSubFolderTestMavenProjectStub"/>
57+
</reactorProjects>
58+
<staleDataPath>${basedir}/target/test/unit/stale-test/target/maven-javadoc-plugin-stale-data.txt</staleDataPath>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>

0 commit comments

Comments
 (0)