-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathScalaMojoSupport.java
725 lines (653 loc) · 26.3 KB
/
ScalaMojoSupport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
* This is free and unencumbered software released into the public domain.
* See UNLICENSE.
*/
package scala_maven;
import java.io.File;
import java.util.*;
import java.util.regex.Pattern;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
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.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.dependency.graph.filter.AncestorOrSelfDependencyNodeFilter;
import org.apache.maven.shared.dependency.graph.filter.AndDependencyNodeFilter;
import org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter;
import org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor;
import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
import org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.util.StringUtils;
import scala_maven_dependency.*;
import scala_maven_executions.JavaMainCaller;
import scala_maven_executions.JavaMainCallerByFork;
import scala_maven_executions.JavaMainCallerInProcess;
import util.FileUtils;
public abstract class ScalaMojoSupport extends AbstractMojo {
/** The maven project. */
@Parameter(property = "project", required = true, readonly = true)
protected MavenProject project;
/**
* The Maven Session Object
*
* <p>Note: Allows extending for 3rd-party usages
*/
@Parameter(property = "session", required = true, readonly = true)
protected MavenSession session;
/** Used to look up Artifacts in the remote repository. */
@Component RepositorySystem factory;
/**
* Additional dependencies/jar to add to classpath to run "scalaClassName" (scope and optional
* field not supported) ex :
*
* <pre>
* <dependencies>
* <dependency>
* <groupId>org.scala-tools</groupId>
* <artifactId>scala-compiler-addon</artifactId>
* <version>1.0-SNAPSHOT</version>
* </dependency>
* </dependencies>
* </pre>
*/
@Parameter protected BasicArtifact[] dependencies;
/**
* Compiler plugin dependencies to use when compiling. ex:
*
* <pre>
* <compilerPlugins>
* <compilerPlugin>
* <groupId>my.scala.plugin</groupId>
* <artifactId>amazingPlugin</artifactId>
* <version>1.0-SNAPSHOT</version>
* </compilerPlugin>
* </compilerPlugins>
* </pre>
*/
@Parameter private BasicArtifact[] compilerPlugins;
/** Jvm Arguments. */
@Parameter protected String[] jvmArgs;
/** compiler additional arguments */
@Parameter protected String[] args;
/**
* Additional parameter to use to call the main class. Use this parameter only from command line
* ("-DaddScalacArgs=arg1|arg2|arg3|..."), not from pom.xml. To define compiler arguments in
* pom.xml see the "args" parameter.
*/
@Parameter(property = "addScalacArgs")
private String addScalacArgs;
/** override the className (FQN) of the scala tool */
@Parameter(required = false, property = "maven.scala.className")
protected String scalaClassName;
/** Scala 's version to use. (property 'maven.scala.version' replaced by 'scala.version') */
@Parameter(property = "scala.version")
private String scalaVersion;
/**
* Organization/group ID of the Scala used in the project. Default value is 'org.scala-lang'. This
* is an advanced setting used for clones of the Scala Language. It should be disregarded in
* standard use cases.
*/
@Parameter(property = "scala.organization", defaultValue = "org.scala-lang")
private String scalaOrganization;
/**
* Scala 's version to use to check binary compatibility (like suffix in artifactId of
* dependency). If it is defined then it is used to checkMultipleScalaVersions
*/
@Parameter(property = "scala.compat.version")
private String scalaCompatVersion;
/** Path to Scala installation to use instead of the artifact (define as dependencies). */
@Parameter(property = "scala.home")
private String scalaHome;
/** Arguments for javac (when using incremental compiler). */
@Parameter(property = "javacArgs")
protected String[] javacArgs;
/**
* Whether to instruct javac to generate debug symbols (when using incremental compiler)
*
* @see <a href=
* "http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#debug">://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#debug</a>
*/
@Parameter(property = "javacGenerateDebugSymbols", defaultValue = "true")
protected boolean javacGenerateDebugSymbols = true;
/**
* Alternative method for specifying javac arguments (when using incremental compiler). Can be
* used from command line with -DaddJavacArgs=arg1|arg2|arg3|... rather than in pom.xml.
*/
@Parameter(property = "addJavacArgs")
protected String addJavacArgs;
/** The -source argument for the Java compiler (when using incremental compiler). */
@Parameter(property = "maven.compiler.source")
protected String source;
/** The -target argument for the Java compiler (when using incremental compiler). */
@Parameter(property = "maven.compiler.target")
protected String target;
/**
* The --release argument for the Java compiler (when using incremental compiler), supported since
* Java9.
*/
@Parameter(property = "maven.compiler.release")
protected String release;
/** The -encoding argument for the Java compiler. (when using incremental compiler). */
@Parameter(property = "project.build.sourceEncoding", defaultValue = "UTF-8")
protected String encoding;
/**
* Display the command line called ? (property 'maven.scala.displayCmd' replaced by 'displayCmd')
*/
@Parameter(property = "displayCmd", defaultValue = "false", required = true)
public boolean displayCmd;
/** Forks the execution of scalac into a separate process. */
@Parameter(defaultValue = "true")
protected boolean fork = true;
/** Force the use of an external ArgFile to run any forked process. */
@Parameter(defaultValue = "false")
protected boolean forceUseArgFile = false;
/** Check if every dependencies use the same version of scala-library or scala.compat.version. */
@Parameter(property = "maven.scala.checkConsistency", defaultValue = "true")
protected boolean checkMultipleScalaVersions;
/**
* Determines if a detection of multiple scala versions in the dependencies will cause the build
* to fail.
*/
@Parameter(defaultValue = "false")
protected boolean failOnMultipleScalaVersions = false;
/**
* Should use CanonicalPath to normalize path (true => getCanonicalPath, false =>
* getAbsolutePath)
*
* @see <a href=
* "https://github.com/davidB/scala-maven-plugin/issues/50">https://github.com/davidB/scala-maven-plugin/issues/50</a>
*/
@Parameter(property = "maven.scala.useCanonicalPath", defaultValue = "true")
protected boolean useCanonicalPath = true;
/** The dependency tree builder to use. */
@Component private DependencyGraphBuilder dependencyGraphBuilder;
/** The toolchain manager to use. */
@Component protected ToolchainManager toolchainManager;
/** List of artifacts to run plugin */
@Parameter(defaultValue = "${plugin.artifacts}")
private List<Artifact> pluginArtifacts;
private MavenArtifactResolver mavenArtifactResolver;
public MavenArtifactResolver findMavenArtifactResolver() {
if (mavenArtifactResolver == null) {
mavenArtifactResolver = new MavenArtifactResolver(factory, session);
}
return mavenArtifactResolver;
}
private Context scalaContext;
public Context findScalaContext() throws Exception {
// reuse/lazy scalaContext creation (doesn't need to be Thread safe, scalaContext should be
// stateless)
if (scalaContext == null) {
VersionNumber scalaVersion = findScalaVersion();
ArtifactIds aids =
scalaVersion.major == 3 ? new ArtifactIds4Scala3(scalaVersion) : new ArtifactIds4Scala2();
VersionNumber requiredScalaVersion =
StringUtils.isNotEmpty(scalaCompatVersion)
? new VersionNumberMask(scalaCompatVersion)
: scalaVersion;
if (requiredScalaVersion.compareTo(scalaVersion) != 0) {
String msg =
String.format(
"Scala library detected %s doesn't match scala.compat.version : %s",
scalaVersion, requiredScalaVersion);
if (failOnMultipleScalaVersions) {
getLog().error(msg);
throw new MojoFailureException(msg);
}
getLog().warn(msg);
}
scalaContext =
StringUtils.isNotEmpty(scalaHome)
? new Context4ScalaHome(scalaVersion, requiredScalaVersion, aids, new File(scalaHome))
: new Context4ScalaRemote(
scalaVersion,
requiredScalaVersion,
aids,
scalaOrganization,
findMavenArtifactResolver());
}
return scalaContext;
}
protected void addToClasspath(
String groupId,
String artifactId,
String version,
String classifier,
Set<File> classpath,
boolean addDependencies)
throws Exception {
MavenArtifactResolver mar = findMavenArtifactResolver();
if (addDependencies) {
for (Artifact a : mar.getJarAndDependencies(groupId, artifactId, version, classifier)) {
classpath.add(a.getFile());
}
} else {
Artifact a = mar.getJar(groupId, artifactId, version, classifier);
classpath.add(a.getFile());
}
}
void addCompilerToClasspath(Set<File> classpath) throws Exception {
Context sc = findScalaContext();
for (Artifact dep : sc.findCompilerAndDependencies()) {
classpath.add(dep.getFile());
}
}
void addLibraryToClasspath(Set<File> classpath) throws Exception {
Context sc = findScalaContext();
for (Artifact dep : sc.findLibraryAndDependencies()) {
classpath.add(dep.getFile());
}
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
String oldWay = System.getProperty("maven.scala.version");
if (oldWay != null) {
getLog().warn("using 'maven.scala.version' is deprecated, use 'scala.version' instead");
if (scalaVersion != null) {
scalaVersion = oldWay;
}
}
oldWay = System.getProperty("maven.scala.displayCmd");
if (oldWay != null) {
getLog().warn("using 'maven.scala.displayCmd' is deprecated, use 'displayCmd' instead");
displayCmd = displayCmd || Boolean.parseBoolean(oldWay);
}
checkScalaVersion();
doExecute();
} catch (MojoExecutionException exc) {
throw exc;
} catch (MojoFailureException | RuntimeException exc) {
throw exc;
} catch (Exception exc) {
throw new MojoExecutionException("wrap: " + exc, exc);
}
}
protected List<Dependency> getDependencies() {
return project.getCompileDependencies();
}
private VersionNumber detectedScalaVersion = null;
private VersionNumber findScalaVersion() throws Exception {
if (detectedScalaVersion == null) {
detectedScalaVersion = findScalaVersion0();
}
return detectedScalaVersion;
}
private VersionNumber findScalaVersion0() throws Exception {
String detectedScalaVersion = scalaVersion;
if (StringUtils.isEmpty(detectedScalaVersion)) {
detectedScalaVersion =
findVersionFromDependencies(scalaOrganization, ArtifactIds.SCALA_LIBRARY_PATTERN);
}
if (StringUtils.isEmpty(detectedScalaVersion)) {
if (!MavenArtifactResolver.POM.equals(project.getPackaging())) {
String error =
String.format(
"%s:%s is missing from project dependencies",
scalaOrganization, ArtifactIds.SCALA_LIBRARY_PATTERN.pattern());
getLog().error(error);
throw new UnsupportedOperationException(error);
}
} else {
// grappy hack to retrieve the SNAPSHOT version without timestamp,...
// because if version is -SNAPSHOT and artifact is deploy with uniqueValue then
// the version
// get from dependency is with the timestamp and a build number (the resolved
// version)
// but scala-compiler with the same version could have different resolved
// version (timestamp,...)
boolean isSnapshot = ArtifactUtils.isSnapshot(detectedScalaVersion);
if (isSnapshot && !detectedScalaVersion.endsWith("-SNAPSHOT")) {
detectedScalaVersion =
detectedScalaVersion.substring(
0,
detectedScalaVersion.lastIndexOf(
'-', detectedScalaVersion.lastIndexOf('-') - 1))
+ "-SNAPSHOT";
}
}
if (StringUtils.isEmpty(detectedScalaVersion)) {
throw new MojoFailureException("no scalaVersion detected or set");
}
if (StringUtils.isNotEmpty(scalaVersion)) {
if (!scalaVersion.equals(detectedScalaVersion)) {
getLog()
.warn(
"scala library version define in dependencies doesn't match the scalaVersion of the plugin");
}
// getLog().info("suggestion: remove the scalaVersion from pom.xml");
// //scalaVersion could be define in a parent pom where lib is not required
}
return new VersionNumber(detectedScalaVersion);
}
// TODO refactor to do only one scan of dependencies to find version
private String findVersionFromDependencies(String groupId, Pattern artifactId) {
VersionNumber version = new VersionNumber("0.0.0");
for (Dependency dep : getDependencies()) {
if (groupId.equals(dep.getGroupId()) && artifactId.matcher(dep.getArtifactId()).find()) {
version = version.max(new VersionNumber(dep.getVersion()));
}
}
if (version.major == 0) {
List<Dependency> deps = new ArrayList<>();
deps.addAll(project.getModel().getDependencies());
if (project.getModel().getDependencyManagement() != null) {
deps.addAll(project.getModel().getDependencyManagement().getDependencies());
}
for (Dependency dep : deps) {
if (groupId.equals(dep.getGroupId()) && artifactId.matcher(dep.getArtifactId()).find()) {
version = version.max(new VersionNumber(dep.getVersion()));
}
}
}
return version.major == 0 ? null : version.toString();
}
void checkScalaVersion() throws Exception {
String sv = findScalaVersion().toString();
if (StringUtils.isNotEmpty(scalaHome)) {
getLog()
.warn(
String.format(
"local scala-library.jar and scala-compiler.jar from scalaHome(%s) used instead of scala %s",
scalaHome, sv));
}
if (checkMultipleScalaVersions) {
checkCorrectVersionsOfScalaLibrary(sv);
}
}
/**
* this method checks to see if there are multiple versions of the scala library
*
* @throws Exception
*/
private void checkCorrectVersionsOfScalaLibrary(String scalaDefVersion) throws Exception {
getLog().debug("Checking for multiple versions of scala");
// TODO - Make sure we handle bad artifacts....
// TODO: note that filter does not get applied due to MNG-3236
VersionNumber sv = new VersionNumber(scalaDefVersion);
VersionNumber requiredScalaVersion =
StringUtils.isNotEmpty(scalaCompatVersion) ? new VersionNumberMask(scalaCompatVersion) : sv;
if (requiredScalaVersion.compareTo(sv) != 0) {
String msg =
String.format(
"Scala library detected %s doesn't match scala.compat.version : %s",
sv, requiredScalaVersion);
if (failOnMultipleScalaVersions) {
getLog().error(msg);
throw new MojoFailureException(msg);
}
getLog().warn(msg);
}
ProjectBuildingRequest request =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
request.setProject(project);
checkArtifactForScalaVersion(
findScalaContext(), dependencyGraphBuilder.buildDependencyGraph(request, null));
}
/** Visits a node (and all dependencies) to see if it contains duplicate scala versions */
private void checkArtifactForScalaVersion(Context scalaContext, DependencyNode rootNode)
throws Exception {
final CheckScalaVersionVisitor visitor = new CheckScalaVersionVisitor(scalaContext, getLog());
CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
DependencyNodeVisitor firstPassVisitor =
new FilteringDependencyNodeVisitor(collectingVisitor, createScalaDistroDependencyFilter());
rootNode.accept(firstPassVisitor);
DependencyNodeFilter secondPassFilter =
new AncestorOrSelfDependencyNodeFilter(collectingVisitor.getNodes());
DependencyNodeVisitor filteredVisitor =
new FilteringDependencyNodeVisitor(visitor, secondPassFilter);
rootNode.accept(filteredVisitor);
if (visitor.isFailed()) {
visitor.logScalaDependents();
if (failOnMultipleScalaVersions) {
getLog().error("Multiple versions of scala libraries detected!");
throw new MojoFailureException("Multiple versions of scala libraries detected!");
}
getLog().warn("Multiple versions of scala libraries detected!");
}
}
/** @return A filter to only extract artifacts deployed from scala distributions */
private DependencyNodeFilter createScalaDistroDependencyFilter() throws Exception {
List<DependencyNodeFilter> filters = new ArrayList<>();
filters.add(new ScalaDistroArtifactFilter(findScalaContext()));
return new AndDependencyNodeFilter(filters);
}
protected abstract void doExecute() throws Exception;
/**
* Get a {@link JavaMainCaller} used invoke a Java process. Typically, this will be one of the
* Scala utilities (Compiler, ScalaDoc, REPL, etc.).
*
* <p>This method does some setup on the {@link JavaMainCaller} which is not done by merely
* invoking {@code new} on one of the implementations. Specifically, it adds any Scala compiler
* plugin options, JVM options, and Scalac options defined on the plugin.
*
* @param forkOverride override the setting for {@link #fork}. Currently, this should only be set
* if you are invoking the REPL.
* @param mainClass the JVM main class to invoke.
* @return a {@link JavaMainCaller} to use to invoke the given command.
*/
final JavaMainCaller getScalaCommand(final boolean forkOverride, final String mainClass)
throws Exception {
JavaMainCaller cmd = getEmptyScalaCommand(mainClass, forkOverride);
cmd.addArgs(args);
if (StringUtils.isNotEmpty(addScalacArgs)) {
cmd.addArgs(StringUtils.split(addScalacArgs, "|"));
}
addCompilerPluginOptions(cmd);
cmd.addJvmArgs(jvmArgs);
return cmd;
}
/**
* Get a {@link JavaMainCaller} used invoke a Java process. Typically this will be one of the
* Scala utilities (Compiler, ScalaDoc, REPL, etc.).
*
* @param mainClass the JVM main class to invoke.
* @return a {@link JavaMainCaller} to use to invoke the given command.
*/
final JavaMainCaller getEmptyScalaCommand(final String mainClass) throws Exception {
return getEmptyScalaCommand(mainClass, fork);
}
/**
* Get a {@link JavaMainCaller} used invoke a Java process. Typically this will be one of the
* Scala utilities (Compiler, ScalaDoc, REPL, etc.).
*
* @param mainClass the JVM main class to invoke.
* @param forkOverride override the setting for {@link #fork}. Currently this should only be set
* if you are invoking the REPL.
* @return a {@link JavaMainCaller} to use to invoke the given command.
*/
private JavaMainCaller getEmptyScalaCommand(final String mainClass, final boolean forkOverride)
throws Exception {
// If we are deviating from the plugin settings, let the user know
// what's going on.
if (forkOverride != fork) {
super.getLog().info("Fork behavior overridden");
super.getLog()
.info(String.format("Fork for this execution is %s.", String.valueOf(forkOverride)));
}
// TODO - Fork or not depending on configuration?
JavaMainCaller cmd;
String toolcp = getToolClasspath();
if (forkOverride) {
// HACK (better may need refactor)
boolean bootcp = true;
if (args != null) {
for (String arg : args) {
bootcp = bootcp && !"-nobootcp".equals(arg);
}
}
String cp = bootcp ? "" : toolcp;
bootcp =
bootcp && !(StringUtils.isNotEmpty(addScalacArgs) && addScalacArgs.contains("-nobootcp"));
// scalac with args in files
// * works only since 2.8.0
// * is buggy (don't manage space in path on windows)
getLog().debug("use java command with args in file forced : " + forceUseArgFile);
cmd =
new JavaMainCallerByFork(
this, mainClass, cp, null, null, forceUseArgFile, getToolchain());
if (bootcp) {
cmd.addJvmArgs("-Xbootclasspath/a:" + toolcp);
}
} else {
cmd = new JavaMainCallerInProcess(this, mainClass, toolcp, null, null);
}
return cmd;
}
protected Toolchain getToolchain() {
return toolchainManager.getToolchainFromBuildContext("jdk", session);
}
private String getToolClasspath() throws Exception {
Set<File> classpath = new TreeSet<>();
addLibraryToClasspath(classpath);
addCompilerToClasspath(classpath);
if (dependencies != null) {
for (BasicArtifact artifact : dependencies) {
addToClasspath(
artifact.groupId, artifact.artifactId, artifact.version, "", classpath, true);
}
}
return FileUtils.toMultiPath(classpath);
}
static String targetOption(String target, VersionNumber scalaVersion) {
if (scalaVersion.major == 2) {
if (scalaVersion.minor <= 12) {
if (target.equals("1.5") || target.equals("5")) {
return "jvm-1.5";
} else if (target.equals("1.6") || target.equals("6")) {
return "jvm-1.6";
} else if (target.equals("1.7") || target.equals("7")) {
return "jvm-1.7";
} else if (target.equals("1.8") || target.equals("8")) {
return "jvm-1.8";
} else {
// invalid or unsupported option, just ignore
return null;
}
} else if (target.equals("1.5")) {
return "5";
} else if (target.equals("1.6")) {
return "6";
} else if (target.equals("1.7")) {
return "7";
} else if (target.equals("1.8")) {
return "8";
}
}
return target;
}
protected List<String> getScalacOptions() throws Exception {
List<String> options = new ArrayList<>();
if (args != null) Collections.addAll(options, args);
if (StringUtils.isNotEmpty(addScalacArgs)) {
Collections.addAll(options, StringUtils.split(addScalacArgs, "|"));
}
options.addAll(getCompilerPluginOptions());
if (target != null && !target.isEmpty()) {
String targetOption = targetOption(target, findScalaVersion());
if (targetOption != null) {
options.add("-target:" + targetOption);
}
}
if (release != null && !release.isEmpty()) {
options.add("-release");
options.add(release);
}
return options;
}
protected List<String> getJavacOptions() {
List<String> options = new ArrayList<>();
if (javacArgs != null) Collections.addAll(options, javacArgs);
if (StringUtils.isNotEmpty(addJavacArgs)) {
Collections.addAll(options, StringUtils.split(addJavacArgs, "|"));
}
// issue #116
if (javacGenerateDebugSymbols) {
options.add("-g");
}
if (release != null && !release.isEmpty()) {
options.add("--release");
options.add(release);
} else {
if (target != null && !target.isEmpty()) {
options.add("-target");
options.add(target);
}
if (source != null && !source.isEmpty()) {
options.add("-source");
options.add(source);
}
}
if (encoding != null) {
options.add("-encoding");
options.add(encoding);
}
return options;
}
/**
* @return This returns whether or not the scala version can support having java sent into the
* compiler
*/
protected boolean isJavaSupportedByCompiler() throws Exception {
return findScalaVersion().compareTo(new VersionNumber("2.7.2")) >= 0;
}
/**
* Adds appropriate compiler plugins to the scalac command.
*
* @param scalac
* @throws Exception
*/
protected void addCompilerPluginOptions(JavaMainCaller scalac) throws Exception {
for (String option : getCompilerPluginOptions()) {
scalac.addArgs(option);
}
}
private List<String> getCompilerPluginOptions() throws Exception {
List<String> options = new ArrayList<>();
for (File plugin : getCompilerPlugins()) {
options.add("-Xplugin:" + plugin.getPath());
}
return options;
}
/**
* Retrieves a list of paths to scala compiler plugins.
*
* @return The list of plugins
* @throws Exception
*/
private Set<File> getCompilerPlugins() throws Exception {
Set<File> plugins = new TreeSet<>();
if (compilerPlugins != null) {
Set<File> ignoreClasspath = new TreeSet<>();
addCompilerToClasspath(ignoreClasspath);
addLibraryToClasspath(ignoreClasspath);
for (BasicArtifact artifact : compilerPlugins) {
getLog().info("compiler plugin: " + artifact.toString());
// TODO - Ensure proper scala version for plugins
Set<File> pluginClassPath = new TreeSet<>();
addToClasspath(
artifact.groupId,
artifact.artifactId,
artifact.version,
artifact.classifier,
pluginClassPath,
false);
pluginClassPath.removeAll(ignoreClasspath);
plugins.addAll(pluginClassPath);
}
}
return plugins;
}
}