Skip to content

Commit 1947181

Browse files
committed
When Scala version not set in plugin configuration or project properties, check direct dependencies (transitive cannot be checked in "generate-resources" Maven lifecycle phase) looking for "org.scala-lang:scala-library" and use its version, if found.
This improvement was suggested in #13.
1 parent 3c9f40e commit 1947181

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/main/java/org/scoverage/plugin/SCoveragePreCompileMojo.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
3030
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
3131
import org.apache.maven.artifact.resolver.ArtifactResolver;
32+
import org.apache.maven.model.Dependency;
3233
import org.apache.maven.plugin.AbstractMojo;
3334
import org.apache.maven.plugin.MojoExecutionException;
3435
import org.apache.maven.plugins.annotations.Component;
@@ -199,26 +200,27 @@ public void execute() throws MojoExecutionException
199200
long ts = System.currentTimeMillis();
200201

201202
String scalaMainVersion = null;
202-
if ( scalaVersion != null )
203+
String resolvedScalaVersion = resolveScalaVersion();
204+
if ( resolvedScalaVersion != null )
203205
{
204-
if ( scalaVersion.startsWith( "2.10." ) )
206+
if ( resolvedScalaVersion.startsWith( "2.10." ) )
205207
{
206208
scalaMainVersion = "2.10";
207209
}
208-
else if ( scalaVersion.startsWith( "2.11." ) )
210+
else if ( resolvedScalaVersion.startsWith( "2.11." ) )
209211
{
210212
scalaMainVersion = "2.11";
211213
}
212214
else
213215
{
214-
getLog().warn( String.format( "Skipping SCoverage execution - unknown Scala version \"%s\"",
215-
scalaVersion ) );
216+
getLog().warn( String.format( "Skipping SCoverage execution - unsupported Scala version \"%s\"",
217+
resolvedScalaVersion ) );
216218
return;
217219
}
218220
}
219221
else
220222
{
221-
getLog().info( "Skipping SCoverage execution - Scala version not set" );
223+
getLog().warn( "Skipping SCoverage execution - Scala version not set" );
222224
return;
223225
}
224226

@@ -318,6 +320,9 @@ else if ( scalaVersion.startsWith( "2.11." ) )
318320

319321
// Private utility methods
320322

323+
private static final String SCALA_LIBRARY_GROUP_ID = "org.scala-lang";
324+
private static final String SCALA_LIBRARY_ARTIFACT_ID = "scala-library";
325+
321326
private static final String DATA_DIR_OPTION = "-P:scoverage:dataDir:";
322327
private static final String EXCLUDED_PACKAGES_OPTION = "-P:scoverage:excludedPackages:";
323328
private static final String EXCLUDED_FILES_OPTION = "-P:scoverage:excludedFiles:";
@@ -332,6 +337,26 @@ private String quoteArgument( String arg )
332337
return arg.indexOf( SPACE ) >= 0 ? DOUBLE_QUOTE + arg + DOUBLE_QUOTE : arg;
333338
}
334339

340+
private String resolveScalaVersion()
341+
{
342+
String result = scalaVersion;
343+
if ( result == null )
344+
{
345+
// check project direct dependencies (transitive dependencies cannot be checked in this Maven lifecycle phase)
346+
List<Dependency> dependencies = project.getDependencies();
347+
for ( Dependency dependency: dependencies )
348+
{
349+
if ( SCALA_LIBRARY_GROUP_ID.equals( dependency.getGroupId() )
350+
&& SCALA_LIBRARY_ARTIFACT_ID.equals( dependency.getArtifactId() ) )
351+
{
352+
result = dependency.getVersion();
353+
break;
354+
}
355+
}
356+
}
357+
return result;
358+
}
359+
335360
private void setProperty( Properties projectProperties, String propertyName, String newValue )
336361
{
337362
if ( projectProperties.containsKey( propertyName ) )

0 commit comments

Comments
 (0)