Skip to content

Commit

Permalink
Enable linter, refactor/cleanup and fix some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jozic committed Nov 12, 2024
1 parent 0089c91 commit 676eb94
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 67 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ under the License.
<version>3.13.0</version>
<configuration>
<release>11</release>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Xlint:-processing</arg>
</compilerArgs>
</configuration>
</plugin>

Expand Down Expand Up @@ -273,9 +277,9 @@ under the License.
<subpackages>org.scoverage.plugin</subpackages>
<links>
<link>https://docs.oracle.com/en/java/javase/11/docs/api/</link>
<link>https://maven.apache.org/ref/${maven.version}/apidocs/</link>
<link>https://maven.apache.org/shared-archives/maven-reporting-api-${maven-reporting-api.version}/apidocs/</link>
<link>https://maven.apache.org/doxia/components/doxia-archives/doxia-${doxia.version}/apidocs/</link>
<link>https://maven.apache.org/ref/${maven.version}/apidocs/</link>
</links>
<notimestamp>true</notimestamp>
</configuration>
Expand Down
102 changes: 38 additions & 64 deletions src/main/java/org/scoverage/plugin/SCoveragePreCompileMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
import javax.inject.Inject;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -106,7 +105,7 @@ public class SCoveragePreCompileMojo
*
* @since 1.0.0
*/
@Parameter( property = "scoverage.excludedPackages", defaultValue = "" )
@Parameter( property = "scoverage.excludedPackages" )
private String excludedPackages;

/**
Expand All @@ -115,7 +114,7 @@ public class SCoveragePreCompileMojo
*
* @since 1.0.0
*/
@Parameter( property = "scoverage.excludedFiles", defaultValue = "" )
@Parameter( property = "scoverage.excludedFiles" )
private String excludedFiles;

/**
Expand Down Expand Up @@ -147,7 +146,7 @@ public class SCoveragePreCompileMojo
*
* @since 1.4.0
*/
@Parameter( property = "scoverage.additionalForkedProjectProperties", defaultValue = "" )
@Parameter( property = "scoverage.additionalForkedProjectProperties" )
private String additionalForkedProjectProperties;

/**
Expand All @@ -171,13 +170,13 @@ public class SCoveragePreCompileMojo
/**
* Artifact factory used to look up artifacts in the remote repository.
*/
@Component
@Inject
private ArtifactFactory factory;

/**
* Artifact resolver used to resolve artifacts.
*/
@Component
@Inject
private ArtifactResolver resolver;

/**
Expand All @@ -192,12 +191,6 @@ public class SCoveragePreCompileMojo
@Parameter( property = "project.remoteArtifactRepositories", readonly = true, required = true )
private List<ArtifactRepository> remoteRepos;

/**
* List of artifacts this plugin depends on.
*/
@Parameter( property = "plugin.artifacts", readonly = true, required = true )
private List<Artifact> pluginArtifacts;

/**
* Configures project for compilation with SCoverage instrumentation.
*
Expand Down Expand Up @@ -231,16 +224,16 @@ public void execute() throws MojoExecutionException

long ts = System.currentTimeMillis();

ScalaVersion resolvedScalaVersion = resolveScalaVersion();
ScalaVersion scalaVersion = resolveScalaVersion();

if ( resolvedScalaVersion != null )
if ( scalaVersion != null )
{
boolean supportedScalaVersion = resolvedScalaVersion.isScala2() && resolvedScalaVersion.isAtLeast( "2.12.8" ) ||
resolvedScalaVersion.isAtLeast( "3.2.0" );
boolean supportedScalaVersion = scalaVersion.isScala2() && scalaVersion.isAtLeast( "2.12.8" ) ||
scalaVersion.isAtLeast( "3.2.0" );
if (!supportedScalaVersion)
{
getLog().warn( String.format( "Skipping SCoverage execution - unsupported Scala version \"%s\". Supported Scala versions are 2.12.8+, 2.13.0+ and 3.2.0+ .",
resolvedScalaVersion.full ) );
scalaVersion.full ) );
return;
}
}
Expand All @@ -254,7 +247,7 @@ public void execute() throws MojoExecutionException
if ( additionalForkedProjectProperties != null && !additionalForkedProjectProperties.isEmpty() )
{
String[] props = additionalForkedProjectProperties.split( ";" );
additionalProjectPropertiesMap = new HashMap<String, String>( props.length );
additionalProjectPropertiesMap = new HashMap<>(props.length);
for ( String propVal: props )
{
String[] tmp = propVal.split( "=", 2 );
Expand All @@ -277,17 +270,16 @@ public void execute() throws MojoExecutionException

try
{
boolean scala2 = resolvedScalaVersion.isScala2();
boolean scala2 = scalaVersion.isScala2();
boolean filePackageExclusionSupportingScala3 =
resolvedScalaVersion.isAtLeast( "3.4.2" ) ||
scalaVersion.isAtLeast( "3.4.2" ) ||
// backported to Scala 3.3 LTS
( resolvedScalaVersion.full.startsWith( "3.3." ) && resolvedScalaVersion.isAtLeast( "3.3.4" ) );
( scalaVersion.full.startsWith( "3.3." ) && scalaVersion.isAtLeast( "3.3.4" ) );

List<Artifact> pluginArtifacts = getScalaScoveragePluginArtifacts( resolvedScalaVersion );
List<Artifact> pluginArtifacts = getScoveragePluginArtifacts( scalaVersion );
if ( scala2 ) // Scala 3 doesn't need scalac-scoverage-runtime
{
Artifact runtimeArtifact = getScalaScoverageRuntimeArtifact( resolvedScalaVersion );
addScoverageDependenciesToClasspath( runtimeArtifact );
addScalacScoverageRuntimeDependencyToClasspath( scalaVersion );
}

String arg = ( scala2 ? SCALA2_DATA_DIR_OPTION : SCALA3_COVERAGE_OUT_OPTION ) + dataDirectory.getAbsolutePath();
Expand Down Expand Up @@ -404,7 +396,6 @@ private ScalaVersion resolveScalaVersion()
if ( result == null || result.isEmpty() )
{
// check project direct dependencies (transitive dependencies cannot be checked in this Maven lifecycle phase)
@SuppressWarnings( "unchecked" )
List<Dependency> dependencies = project.getDependencies();
for ( Dependency dependency: dependencies )
{
Expand Down Expand Up @@ -445,55 +436,46 @@ private void setProperty( Properties projectProperties, String propertyName, Str
}
}

private ArtifactVersion getScalacPluginVersion() {
if ( scalacPluginVersion == null || scalacPluginVersion.isEmpty()) {
private String getScalacPluginVersion() {
if ( StringUtils.isEmpty(scalacPluginVersion) ) {
throw new IllegalStateException("scalacPluginVersion is unset.");
} else if ( scalacPluginVersion.startsWith("1.") ) {
throw new IllegalStateException( String.format( "Unsupported scalacPluginVersion \"%s\". Please use scalacPluginVersion 2.0.0+ or use older version of scoverage-maven-plugin", scalacPluginVersion ) );
} else {
return new DefaultArtifactVersion(scalacPluginVersion);
return scalacPluginVersion;
}
}

private List<Artifact> getScalaScoveragePluginArtifacts( ScalaVersion resolvedScalaVersion )
private List<Artifact> getScoveragePluginArtifacts(ScalaVersion scalaVersion )
throws ArtifactNotFoundException, ArtifactResolutionException
{
String resolvedScalacPluginVersion = getScalacPluginVersion().toString();
List<Artifact> resolvedArtifacts = new ArrayList<>();
if ( resolvedScalaVersion.isScala2() ) // Scala 3 doesn't need scalac-scoverage-plugin
if ( scalaVersion.isScala2() ) // Scala 3 doesn't need scalac-scoverage-plugin
{
resolvedArtifacts.add(getResolvedArtifact("org.scoverage", "scalac-scoverage-plugin_" + resolvedScalaVersion.full, resolvedScalacPluginVersion));
resolvedArtifacts.add(resolveScoverageArtifact("scalac-scoverage-plugin_" + scalaVersion.full ));
}
resolvedArtifacts.add(getResolvedArtifact("org.scoverage", "scalac-scoverage-domain_" + resolvedScalaVersion.compatible, resolvedScalacPluginVersion));
resolvedArtifacts.add(getResolvedArtifact("org.scoverage", "scalac-scoverage-serializer_" + resolvedScalaVersion.compatible, resolvedScalacPluginVersion));
resolvedArtifacts.add(resolveScoverageArtifact("scalac-scoverage-domain_" + scalaVersion.compatible ));
resolvedArtifacts.add(resolveScoverageArtifact("scalac-scoverage-serializer_" + scalaVersion.compatible ));
return resolvedArtifacts;
}

private Artifact getScalaScoverageRuntimeArtifact( ScalaVersion resolvedScalaVersion )
throws ArtifactNotFoundException, ArtifactResolutionException
{
return getResolvedArtifact(
"org.scoverage", "scalac-scoverage-runtime_" + resolvedScalaVersion.compatible,
getScalacPluginVersion().toString() );
}

/**
* We need to tweak our test classpath for Scoverage.
*
* @throws MojoExecutionException
*/
private void addScoverageDependenciesToClasspath( Artifact scalaScoveragePluginArtifact )
{
@SuppressWarnings( "unchecked" )
Set<Artifact> set = new LinkedHashSet<Artifact>( project.getDependencyArtifacts() );
set.add( scalaScoveragePluginArtifact );
private void addScalacScoverageRuntimeDependencyToClasspath(ScalaVersion resolvedScalaVersion )
throws ArtifactResolutionException, ArtifactNotFoundException {

Set<Artifact> set = new LinkedHashSet<>(project.getDependencyArtifacts());
set.add(resolveScoverageArtifact( "scalac-scoverage-runtime_" + resolvedScalaVersion.compatible) );
project.setDependencyArtifacts( set );
}

private Artifact getResolvedArtifact( String groupId, String artifactId, String version )
private Artifact resolveScoverageArtifact( String artifactId )
throws ArtifactNotFoundException, ArtifactResolutionException
{
Artifact artifact = factory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar" );
Artifact artifact = factory.createArtifact(
"org.scoverage", artifactId, getScalacPluginVersion(), Artifact.SCOPE_COMPILE, "jar"
);
resolver.resolve( artifact, remoteRepos, localRepo );
return artifact;
}
Expand All @@ -509,21 +491,13 @@ private void saveSourceRootsToFile() throws IOException
dataDirectory.getAbsolutePath() ) );
}
File sourceRootsFile = new File( dataDirectory, "source.roots" );
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter( new FileOutputStream( sourceRootsFile ), "UTF-8" ) );
try
{
for ( String sourceRoot: sourceRoots )
{
try ( BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter( new FileOutputStream(sourceRootsFile), StandardCharsets.UTF_8 ))) {
for ( String sourceRoot : sourceRoots ) {
writer.write( sourceRoot );
writer.newLine();
}
}
finally
{
writer.close();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public void execute()

try
{
RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html", null );
SiteRendererSink sink = new SiteRendererSink( context );
Locale locale = Locale.getDefault();
generate( sink, locale );
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/scoverage/plugin/ScalaVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public ScalaVersion(String s) {
// compute compatible
compatible =
modifier != null ? full : // non-stable versions are not compatible with anything else
isScala2() ? major + "." + minor : // Scala 2.X.Y is compatible with any Scala 2.X.Z
major == 2 ? major + "." + minor : // Scala 2.X.Y is compatible with any Scala 2.X.Z
major + ""; // Scala 3.X is compatible with any Scala 3.Y
}

Expand Down

0 comments on commit 676eb94

Please sign in to comment.