Skip to content

Commit

Permalink
[JXR-173] Upgrade Maven Reporting API to 3.1.1/Maven Reporting Impl t…
Browse files Browse the repository at this point in the history
…o 3.2.0

This plugin hasn't been touched testwise for a very long time. The testing
approach from MPIR has been applied to accommodate M-R-Impl 3.2.0 and future
versions of it.

This also upgrades for ITs:
* Maven Site Plugin to 3.12.1
* Maven Javadoc Plugin to 3.4.1

This closes #66
  • Loading branch information
michael-o committed Aug 14, 2022
1 parent 92a5d69 commit f2c0657
Show file tree
Hide file tree
Showing 25 changed files with 355 additions and 173 deletions.
45 changes: 37 additions & 8 deletions maven-jxr-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ under the License.

<properties>
<mavenVersion>3.2.5</mavenVersion>
<aetherVersion>1.0.0.v20140518</aetherVersion>
</properties>

<prerequisites>
Expand Down Expand Up @@ -85,16 +86,15 @@ under the License.
<artifactId>wagon-provider-api</artifactId>
<version>2.8</version><!-- like Maven 3.2.5 -->
</dependency>
<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-api</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-impl</artifactId>
<version>3.1.0</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
</exclusions>
<version>3.2.0</version>
</dependency>

<!-- shared utils -->
Expand Down Expand Up @@ -126,6 +126,36 @@ under the License.
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-wagon</artifactId>
<version>${aetherVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http-lightweight</artifactId>
<version>3.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -189,7 +219,6 @@ under the License.
<artifactId>maven-invoker-plugin</artifactId>
<configuration>
<writeJunitReport>true</writeJunitReport>
<projectsDirectory>src/it</projectsDirectory>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,19 @@
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.jxr.JXR;
import org.apache.maven.jxr.JavaCodeTransform;
import org.apache.maven.jxr.JxrException;
import org.apache.maven.jxr.pacman.FileManager;
import org.apache.maven.jxr.pacman.PackageManager;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.languages.java.version.JavaVersion;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;

/**
Expand All @@ -60,31 +57,9 @@
public abstract class AbstractJxrReport
extends AbstractMavenReport
{
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;

@Component
private Renderer siteRenderer;

/**
* Output folder where the main page of the report will be generated. Note that this parameter is only relevant if
* the goal is run directly from the command line or from the default lifecycle. If the goal is run indirectly as
* part of a site generation, the output directory configured in the Maven Site Plugin will be used instead.
*/
@Parameter( defaultValue = "${project.reporting.outputDirectory}", required = true )
private File outputDirectory;

/**
* File input encoding.
*/
@Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
private String inputEncoding;

/**
* File output encoding.
*/
@Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
private String outputEncoding;
@Parameter( defaultValue = "${session}", readonly = true, required = true )
private MavenSession session;

/**
* Title of window of the Xref HTML files.
Expand Down Expand Up @@ -171,18 +146,6 @@ public abstract class AbstractJxrReport
*/
private JavaVersion javadocTemplatesVersion;

/**
* Gets the effective reporting output files encoding.
*
* @return The effective reporting output file encoding, never <code>null</code>: defaults to <code>UTF-8</code>
* instead.
*/
@Override
protected String getOutputEncoding()
{
return ( outputEncoding == null ) ? ReaderFactory.UTF_8 : outputEncoding;
}

/**
* Compiles the list of directories which contain source files that will be included in the JXR report generation.
*
Expand Down Expand Up @@ -270,16 +233,10 @@ private void createXref( Locale locale, String destinationDirectory, List<String
FileManager fileManager = new FileManager();
PackageManager packageManager = new PackageManager( fileManager );
JavaCodeTransform codeTransform = new JavaCodeTransform( packageManager, fileManager );

JXR jxr = new JXR( packageManager, codeTransform );
jxr.setDest( Paths.get( destinationDirectory ) );
if ( StringUtils.isEmpty( inputEncoding ) )
{
String platformEncoding = System.getProperty( "file.encoding" );
getLog().warn( "File encoding has not been set, using platform encoding " + platformEncoding
+ ", i.e. build is platform dependent!" );
}
jxr.setInputEncoding( inputEncoding );
jxr.setInputEncoding( getInputEncoding() );
jxr.setLocale( locale );
jxr.setOutputEncoding( getOutputEncoding() );
jxr.setRevision( "HEAD" );
Expand Down Expand Up @@ -457,21 +414,14 @@ private void copyResources( String dir, String sourceFolder, String... files )
}

@Override
protected Renderer getSiteRenderer()
protected MavenProject getProject()
{
return siteRenderer;
}

@Override
protected String getOutputDirectory()
{
return outputDirectory.getAbsolutePath();
return project;
}

@Override
public MavenProject getProject()
protected MavenSession getSession()
{
return project;
return session;
}

/**
Expand Down Expand Up @@ -500,33 +450,6 @@ protected boolean canGenerateReport( List<String> sourceDirs )
return canGenerate;
}

/*
* This is called for a standalone execution. Well, that's the claim. It also ends up called for the aggregate mojo,
* since that is configured as an execution, not in the reporting section, at least by some people on some days. We
* do NOT want the default behavior.
*/
@Override
public void execute()
throws MojoExecutionException
{

if ( skip )
{
getLog().info( "Skipping JXR." );
return;
}

Locale locale = Locale.getDefault();
try
{
executeReport( locale );
}
catch ( MavenReportException e )
{
throw new MojoExecutionException( "Error generating JXR report", e );
}
}

@Override
protected void executeReport( Locale locale )
throws MavenReportException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package org.apache.maven.plugin.jxr;

/*
* 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.nio.file.Files;

import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.ArtifactStubFactory;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.repository.LocalRepository;

/**
* Abstract class to test reports generation.
*/
public abstract class AbstractJxrTestCase
extends AbstractMojoTestCase
{
private ArtifactStubFactory artifactStubFactory;

/**
* The current project to be test.
*/
private MavenProject testMavenProject;

@Override
protected void setUp()
throws Exception
{
// required for mojo lookups to work
super.setUp();

artifactStubFactory = new DependencyArtifactStubFactory( getTestFile( "target" ), true, false );
artifactStubFactory.getWorkingDir().mkdirs();
}

@Override
protected void tearDown()
throws Exception
{
super.tearDown();
}

/**
* Get the current Maven project
*
* @return the maven project
*/
protected MavenProject getTestMavenProject()
{
return testMavenProject;
}

/**
* Get the generated report as file in the test maven project.
*
* @param name the name of the report.
* @return the generated report as file
* @throws IOException if the return file doesnt exist
*/
protected File getGeneratedReport( String name )
throws IOException
{
String outputDirectory = getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId();

File report = new File( outputDirectory, name );
if ( !report.exists() )
{
throw new IOException( "File not found. Attempted: " + report );
}

return report;
}

/**
* Generate the report and return the generated file
*
* @param goal the mojo goal.
* @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/".
* @return the generated HTML file
* @throws Exception if any
*/
protected File generateReport( String goal, String pluginXml )
throws Exception
{
File pluginXmlFile = new File( getBasedir(), "src/test/resources/unit/" + pluginXml );
AbstractJxrReport mojo = createReportMojo( goal, pluginXmlFile );
return generateReport( mojo, pluginXmlFile );
}

protected AbstractJxrReport createReportMojo( String goal, File pluginXmlFile )
throws Exception
{
AbstractJxrReport mojo = (AbstractJxrReport) lookupMojo( goal, pluginXmlFile );
assertNotNull( "Mojo not found.", mojo );

LegacySupport legacySupport = lookup( LegacySupport.class );
legacySupport.setSession( newMavenSession( new MavenProjectStub() ) );
DefaultRepositorySystemSession repoSession =
(DefaultRepositorySystemSession) legacySupport.getRepositorySession();
repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, new LocalRepository( artifactStubFactory.getWorkingDir() ) ) );

setVariableValueToObject( mojo, "session", legacySupport.getSession() );
setVariableValueToObject( mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories() );
return mojo;
}

protected File generateReport( AbstractJxrReport mojo, File pluginXmlFile )
throws Exception
{
mojo.execute();

ProjectBuilder builder = lookup( ProjectBuilder.class );

ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
buildingRequest.setRepositorySession( lookup( LegacySupport.class ).getRepositorySession() );

testMavenProject = builder.build( pluginXmlFile, buildingRequest ).getProject();

File outputDir = mojo.getReportOutputDirectory();
String filename = mojo.getOutputName() + ".html";

return new File( outputDir, filename );
}

/**
* Read the contents of the specified file object into a string
*/
protected String readFile( File xrefTestDir, String fileName ) throws IOException
{
return new String( Files.readAllBytes( xrefTestDir.toPath().resolve( fileName ) ) );
}

}
Loading

0 comments on commit f2c0657

Please sign in to comment.