Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[MJAVADOC-626] Add a stale javadoc detection mechanism #33

Merged
merged 2 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ under the License.
</distributionManagement>

<properties>
<javaVersion>7</javaVersion>
<javaVersion>8</javaVersion>
gnodet marked this conversation as resolved.
Show resolved Hide resolved
<mavenVersion>3.0</mavenVersion>
<doxiaVersion>1.7</doxiaVersion>
<doxia-sitetoolsVersion>1.7.4</doxia-sitetoolsVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,18 @@ public abstract class AbstractJavadocMojo
@Parameter
private Map<String, String> jdkToolchain;

/**
* <p>
* Location of the file used to store the state of the previous javadoc run.
* This is used to skip the generation if nothing has changed.
* </p>
*
* @since 3.2.0
*/
@Parameter( property = "staleDataPath",
defaultValue = "${project.build.directory}/maven-javadoc-plugin-stale-data.txt" )
private File staleDataPath;

// ----------------------------------------------------------------------
// protected methods
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -5701,6 +5713,77 @@ private void addTagletsFromTagletArtifacts( List<String> arguments )
* @throws MavenReportException if any errors occur
*/
private void executeJavadocCommandLine( Commandline cmd, File javadocOutputDirectory )
throws MavenReportException
{
if ( staleDataPath != null )
{
if ( !isUpToDate( cmd ) )
{
doExecuteJavadocCommandLine( cmd, javadocOutputDirectory );
StaleHelper.writeStaleData( cmd, staleDataPath.toPath() );
}
}
else
{
doExecuteJavadocCommandLine( cmd, javadocOutputDirectory );
}
}

/**
* Check if the javadoc is uptodate or not
*
* @param cmd not null
* @return <code>true</code> is the javadoc is uptodate, <code>false</code> otherwise
* @throws MavenReportException if any error occur
*/
private boolean isUpToDate( Commandline cmd )
throws MavenReportException
{
try
{
String curdata = StaleHelper.getStaleData( cmd );
Path cacheData = staleDataPath.toPath();
String prvdata;
if ( Files.isRegularFile( cacheData ) )
{
prvdata = new String( Files.readAllBytes( cacheData ), StandardCharsets.UTF_8 );
}
else
{
prvdata = null;
}
if ( curdata.equals( prvdata ) )
{
getLog().info( "Skipping javadoc generation, everything is up to date." );
return true;
}
else
{
if ( prvdata == null )
{
getLog().info( "No previous run data found, generating javadoc." );
}
else
{
getLog().info( "Configuration changed, re-generating javadoc." );
}
}
}
catch ( IOException e )
{
throw new MavenReportException( "Error checking uptodate status", e );
}
return false;
}

/**
* Execute the Javadoc command line
*
* @param cmd not null
* @param javadocOutputDirectory not null
* @throws MavenReportException if any errors occur
*/
private void doExecuteJavadocCommandLine( Commandline cmd, File javadocOutputDirectory )
throws MavenReportException
{
if ( getLog().isDebugEnabled() )
Expand Down
169 changes: 169 additions & 0 deletions src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package org.apache.maven.plugins.javadoc;

/*
* 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.
*/

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.util.cli.Commandline;

/**
* Helper class to compute and write data used to detect a
* stale javadoc.
*/
public class StaleHelper
{

/**
* Compute the data used to detect a stale javadoc
*
* @param cmd the command line
* @return the stale data
* @throws MavenReportException if an error occurs
*/
public static String getStaleData( Commandline cmd )
throws MavenReportException
{
try
{
List<String> ignored = new ArrayList<>();
List<String> options = new ArrayList<>();
Path dir = cmd.getWorkingDirectory().toPath().toAbsolutePath().normalize();
String[] args = cmd.getCommandline();
Collections.addAll( options, args );
for ( String arg : args )
{
if ( arg.startsWith( "@" ) )
{
String name = arg.substring( 1 );
Files.lines( dir.resolve( name ) ).forEachOrdered( options::add );
ignored.add( name );
}
}
List<String> state = new ArrayList<>( options );
boolean cp = false;
boolean sp = false;
for ( String arg : options )
{
if ( cp )
{
String s = unquote( arg );
Stream.of( s.split( File.pathSeparator ) )
.map( dir::resolve )
.map( p -> p + " = " + lastmod( p ) )
.forEachOrdered( state::add );
}
else if ( sp )
{
String s = unquote( arg );
Stream.of( s.split( File.pathSeparator ) )
.map( dir::resolve )
.flatMap( StaleHelper::walk )
.filter( Files::isRegularFile )
.map( p -> p + " = " + lastmod( p ) )
.forEachOrdered( state::add );
}
cp = "-classpath".equals( arg );
sp = "-sourcepath".equals( arg );
}
walk( dir )
.filter( Files::isRegularFile )
.filter( p -> !ignored.contains( p.getFileName().toString() ) )
.map( p -> p + " = " + lastmod( p ) )
.forEachOrdered( state::add );

return String.join( SystemUtils.LINE_SEPARATOR, state );
}
catch ( Exception e )
{
throw new MavenReportException( "Unable to compute stale date", e );
}
}

/**
* Write the data used to detect a stale javadoc
*
* @param cmd the command line
* @param path the stale data path
* @throws MavenReportException if an error occurs
*/
public static void writeStaleData( Commandline cmd, Path path )
throws MavenReportException
{
try
{
String curdata = getStaleData( cmd );
Files.createDirectories( path.getParent() );
try ( Writer w = Files.newBufferedWriter( path ) )
{
w.append( curdata );
}
}
catch ( IOException e )
{
throw new MavenReportException( "Error checking stale data", e );
}
}

private static Stream<Path> walk( Path dir )
{
try
{
return Files.walk( dir );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}

private static String unquote( String s )
{
if ( s.startsWith( "'" ) && s.endsWith( "'" ) )
{
return s.substring( 1, s.length() - 1 ).replaceAll( "\\\\'", "'" );
}
else
{
return s;
}
}

private static long lastmod( Path p )
{
try
{
return Files.getLastModifiedTime( p ).toMillis();
}
catch ( IOException e )
{
return 0;
}
}

}