Skip to content

Commit

Permalink
Use project's compile root dirs instead of hardcoded source dir paths
Browse files Browse the repository at this point in the history
  • Loading branch information
aloubyansky committed Mar 10, 2022
1 parent bab16b9 commit 1f884cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;
import java.util.function.Consumer;

Expand Down Expand Up @@ -50,12 +50,11 @@ protected boolean beforeExecute() throws MojoExecutionException, MojoFailureExce

@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException {
String projectDir = mavenProject().getBasedir().getAbsolutePath();
Path sourcesDir = Paths.get(projectDir, "src", "main");
generateCode(sourcesDir, path -> mavenProject().addCompileSourceRoot(path.toString()), false);
generateCode(getParentDirs(mavenProject().getCompileSourceRoots()),
path -> mavenProject().addCompileSourceRoot(path.toString()), false);
}

void generateCode(Path sourcesDir,
void generateCode(PathCollection sourceParents,
Consumer<Path> sourceRegistrar,
boolean test) throws MojoFailureException, MojoExecutionException {

Expand All @@ -78,7 +77,7 @@ void generateCode(Path sourcesDir,
Path.class, Path.class,
Consumer.class, ApplicationModel.class, Properties.class, String.class,
boolean.class);
initAndRun.invoke(null, deploymentClassLoader, PathList.of(sourcesDir),
initAndRun.invoke(null, deploymentClassLoader, sourceParents,
generatedSourcesDir(test), buildDir().toPath(),
sourceRegistrar, curatedApplication.getApplicationModel(), mavenProject().getProperties(),
launchMode.name(),
Expand All @@ -94,6 +93,20 @@ void generateCode(Path sourcesDir,
}
}

protected PathCollection getParentDirs(List<String> sourceDirs) {
if (sourceDirs.size() == 1) {
return PathList.of(Path.of(sourceDirs.get(0)).getParent());
}
final PathList.Builder builder = PathList.builder();
for (int i = 0; i < sourceDirs.size(); ++i) {
final Path parentDir = Path.of(sourceDirs.get(i)).getParent();
if (!builder.contains(parentDir)) {
builder.add(parentDir);
}
}
return builder.build();
}

private Path generatedSourcesDir(boolean test) {
return test ? buildDir().toPath().resolve("generated-test-sources") : buildDir().toPath().resolve("generated-sources");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.quarkus.maven;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -13,8 +10,7 @@
public class GenerateCodeTestsMojo extends GenerateCodeMojo {
@Override
protected void doExecute() throws MojoExecutionException, MojoFailureException {
String projectDir = mavenProject().getBasedir().getAbsolutePath();
Path testSources = Paths.get(projectDir, "src", "test");
generateCode(testSources, path -> mavenProject().addTestCompileSourceRoot(path.toString()), true);
generateCode(getParentDirs(mavenProject().getTestCompileSourceRoots()),
path -> mavenProject().addTestCompileSourceRoot(path.toString()), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public AppModelResolver getAppModelResolver() {
if (mavenArtifactResolver == null) {
final BootstrapMavenContext mvnCtx = createBootstrapMavenContext();
if (managingProject == null) {
managingProject = mvnCtx.getCurrentProjectArtifact("pom");
managingProject = mvnCtx.getCurrentProjectArtifact(ArtifactCoords.TYPE_POM);
}
mvn = new MavenArtifactResolver(mvnCtx);
} else {
Expand Down

0 comments on commit 1f884cf

Please sign in to comment.