11package org .antipathy .mvn_scalafmt ;
22
33import org .antipathy .mvn_scalafmt .model .Summary ;
4- import org .apache .maven .model .Build ;
54import org .apache .maven .plugin .AbstractMojo ;
65import org .apache .maven .plugin .MojoExecutionException ;
76import org .apache .maven .plugins .annotations .LifecyclePhase ;
1110import org .apache .maven .model .Repository ;
1211
1312import java .io .File ;
14- import java .io .IOException ;
1513import java .util .ArrayList ;
16- import java .util .HashSet ;
1714import java .util .List ;
1815
1916/**
@@ -28,9 +25,9 @@ public class FormatMojo extends AbstractMojo {
2825 private boolean skipTestSources ;
2926 @ Parameter (property = "format.skipSources" , defaultValue = "false" )
3027 private boolean skipSources ;
31- @ Parameter ()
28+ @ Parameter (defaultValue = "${project.build.sourceDirectory}/../scala" , required = true )
3229 private List <File > sourceDirectories ;
33- @ Parameter ()
30+ @ Parameter (defaultValue = "${project.build.testSourceDirectory}/../scala" , required = true )
3431 private List <File > testSourceDirectories ;
3532 @ Parameter (property = "format.respectVersion" , defaultValue = "false" , required = true )
3633 private boolean respectVersion ;
@@ -65,13 +62,19 @@ private List<String> getRepositoriesUrls(List<Repository> repositories) {
6562
6663 public void execute () throws MojoExecutionException {
6764
68- List <File > sources ;
69- try {
70- sources = getSources ();
71- } catch (IOException exception ) {
72- throw new MojoExecutionException ("Couldn't determine canonical sources" , exception );
65+ List <File > sources = new ArrayList <>();
66+
67+ if (!skipSources ) {
68+ sources .addAll (sourceDirectories );
69+ } else {
70+ getLog ().warn ("format.skipSources set, ignoring main directories" );
7371 }
7472
73+ if (!skipTestSources ) {
74+ sources .addAll (testSourceDirectories );
75+ } else {
76+ getLog ().warn ("format.skipTestSources set, ignoring validateOnly directories" );
77+ }
7578 if (!sources .isEmpty ()) {
7679 try {
7780
@@ -84,9 +87,7 @@ public void execute() throws MojoExecutionException {
8487 showReformattedOnly ,
8588 branch ,
8689 project .getBasedir (),
87- useSpecifiedRepositories ?
88- getRepositoriesUrls (project .getRepositories ()) :
89- new ArrayList <String >()
90+ useSpecifiedRepositories ? getRepositoriesUrls (mavenRepositories ) : new ArrayList <String >()
9091 ).format (sources );
9192 getLog ().info (result .toString ());
9293 if (validateOnly && result .unformattedFiles () != 0 ) {
@@ -100,51 +101,4 @@ public void execute() throws MojoExecutionException {
100101 getLog ().warn ("No sources specified, skipping formatting" );
101102 }
102103 }
103-
104- private List <File > getSources () throws IOException {
105- HashSet <File > sources = new HashSet <>();
106- Build build = project .getBuild ();
107-
108- if (skipSources ) {
109- getLog ().warn ("format.skipSources set, ignoring main directories" );
110- } else if (sourceDirectories == null || sourceDirectories .isEmpty ()) {
111- appendCanonicalSources (
112- sources ,
113- project .getCompileSourceRoots (),
114- build .getSourceDirectory ()
115- );
116- } else {
117- sources .addAll (sourceDirectories );
118- }
119-
120- if (skipTestSources ) {
121- getLog ().warn ("format.skipTestSources set, ignoring validateOnly directories" );
122- } else if (testSourceDirectories == null || testSourceDirectories .isEmpty ()) {
123- appendCanonicalSources (
124- sources ,
125- project .getTestCompileSourceRoots (),
126- build .getTestSourceDirectory ()
127- );
128- } else {
129- sources .addAll (testSourceDirectories );
130- }
131-
132- return new ArrayList <>(sources );
133- }
134-
135- private void appendCanonicalSources (
136- HashSet <File > sources ,
137- List <String > sourceRoots ,
138- String defaultSource
139- ) throws IOException {
140- for (String source : sourceRoots ) {
141- sources .add (getCanonicalFile (source ));
142- }
143- sources .add (getCanonicalFile (defaultSource + "/../scala" ));
144- }
145-
146- private File getCanonicalFile (String relative ) throws IOException {
147- return new File (project .getBasedir (), relative ).getCanonicalFile ();
148- }
149-
150104}
0 commit comments