Skip to content

Commit

Permalink
[MARTIFACT-78] diagnose reactor root and first
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Dec 21, 2024
1 parent 1a30172 commit 5b81b72
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected AbstractBuildinfoMojo(
public void execute() throws MojoExecutionException {
boolean mono = session.getProjects().size() == 1;

hasBadOutputTimestamp(outputTimestamp, getLog(), project, session.getProjects(), diagnose);
hasBadOutputTimestamp(outputTimestamp, getLog(), project, session, diagnose);

if (!mono) {
// if module skips install and/or deploy
Expand All @@ -179,17 +179,13 @@ public void execute() throws MojoExecutionException {
}

static boolean hasBadOutputTimestamp(
String outputTimestamp,
Log log,
MavenProject project,
List<MavenProject> reactorProjects,
boolean diagnose) {
String outputTimestamp, Log log, MavenProject project, MavenSession session, boolean diagnose) {
Instant timestamp =
MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).orElse(null);
String effective = ((timestamp == null) ? "disabled" : DateTimeFormatter.ISO_INSTANT.format(timestamp));

if (diagnose) {
diagnose(outputTimestamp, log, project, reactorProjects, effective);
diagnose(outputTimestamp, log, project, session, effective);
}

if (timestamp == null) {
Expand All @@ -215,7 +211,7 @@ static boolean hasBadOutputTimestamp(
// check if timestamp defined in a project from reactor: info if it is not the case
boolean parentInReactor = false;
MavenProject reactorParent = project;
while (reactorProjects.contains(reactorParent.getParent())) {
while (session.getProjects().contains(reactorParent.getParent())) {
parentInReactor = true;
reactorParent = reactorParent.getParent();
}
Expand All @@ -231,11 +227,18 @@ static boolean hasBadOutputTimestamp(
}

static void diagnose(
String outputTimestamp,
Log log,
MavenProject project,
List<MavenProject> reactorProjects,
String effective) {
String outputTimestamp, Log log, MavenProject project, MavenSession session, String effective) {
if (session.getProjects().size() > 1) {
log.info("reactor executionRoot = " + session.getTopLevelProject().getId());
log.info("reactor first = " + session.getProjects().get(0).getId());
MavenProject parent = session.getProjects().get(0).getParent();
log.info("reactor first parent = " + ((parent == null) ? parent : parent.getId()));
if (parent != null && session.getProjects().contains(parent)) {
// should not happen...
log.warn("reactor first parent = " + parent.getId() + " is in reactor");
}
}

log.info("outputTimestamp = " + outputTimestamp
+ (effective.equals(outputTimestamp) ? "" : (" => " + effective)));

Expand All @@ -254,14 +257,18 @@ static void diagnose(
+ " - project.build.outputTimestamp property from project original model = "
+ originalModelProperty);

if (outputTimestamp == null) {
return;
}

MavenProject parent = project.getParent();
if (parent != null) {
StringBuilder sb = new StringBuilder("Inheritance analysis property:" + System.lineSeparator()
+ " - current " + project.getId() + " property = " + projectProperty);
while (parent != null) {
String parentProperty = parent.getProperties().getProperty("project.build.outputTimestamp");
sb.append(System.lineSeparator());
sb.append(" - " + (reactorProjects.contains(parent) ? "reactor" : "external") + " parent "
sb.append(" - " + (session.getProjects().contains(parent) ? "reactor" : "external") + " parent "
+ parent.getId() + " property = " + parentProperty);
if (!projectProperty.equals(parentProperty)) {
break;
Expand Down Expand Up @@ -291,7 +298,7 @@ protected void copyAggregateToRoot(File aggregate) throws MojoExecutionException
}

// copy aggregate file to root target directory
MavenProject root = getExecutionRoot();
MavenProject root = session.getTopLevelProject();
String extension = aggregate.getName().substring(aggregate.getName().lastIndexOf('.'));
File rootCopy =
new File(root.getBuild().getDirectory(), root.getArtifactId() + '-' + root.getVersion() + extension);
Expand Down Expand Up @@ -325,7 +332,7 @@ protected BuildInfoWriter newBuildInfoWriter(PrintWriter p, boolean mono) {
* @throws MojoExecutionException if anything goes wrong
*/
protected Map<Artifact, String> generateBuildinfo(boolean mono) throws MojoExecutionException {
MavenProject root = mono ? project : getExecutionRoot();
MavenProject root = mono ? project : session.getTopLevelProject();

buildinfoFile.getParentFile().mkdirs();

Expand Down Expand Up @@ -355,15 +362,6 @@ protected Map<Artifact, String> generateBuildinfo(boolean mono) throws MojoExecu
}
}

protected MavenProject getExecutionRoot() {
for (MavenProject p : session.getAllProjects()) {
if (p.isExecutionRoot()) {
return p;
}
}
return null;
}

private MavenProject getLastProject() {
int i = session.getProjects().size();
while (i > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ protected MavenExecutionPlan calculateExecutionPlan() throws MojoExecutionExcept

@Override
public void execute() throws MojoExecutionException {
boolean fail = AbstractBuildinfoMojo.hasBadOutputTimestamp(
outputTimestamp, getLog(), project, session.getProjects(), diagnose);
boolean fail =
AbstractBuildinfoMojo.hasBadOutputTimestamp(outputTimestamp, getLog(), project, session, diagnose);

// TODO check maven-jar-plugin module-info.class?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected void skip(MavenProject last) throws MojoExecutionException {
* @throws MojoExecutionException if anything goes wrong
*/
private void checkAgainstReference(Map<Artifact, String> artifacts, boolean mono) throws MojoExecutionException {
MavenProject root = mono ? project : getExecutionRoot();
MavenProject root = mono ? project : session.getTopLevelProject();
File referenceDir = new File(root.getBuild().getDirectory(), "reference");
referenceDir.mkdirs();

Expand Down Expand Up @@ -301,7 +301,7 @@ private String getRepositoryFilename(Artifact a) {
}

private String relative(File file) {
File basedir = getExecutionRoot().getBasedir();
File basedir = session.getTopLevelProject().getBasedir();
int length = basedir.getPath().length();
String path = file.getPath();
return path.substring(length + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void execute() throws MojoExecutionException {
MavenArchiver.parseBuildOutputTimestamp(outputTimestamp).orElse(null);
String effective = ((timestamp == null) ? "disabled" : DateTimeFormatter.ISO_INSTANT.format(timestamp));

diagnose(outputTimestamp, getLog(), project, session.getProjects(), effective);
diagnose(outputTimestamp, getLog(), project, session, effective);
getLog().info("");
describeBuildOutput();
}
Expand All @@ -81,7 +81,7 @@ public void execute() throws MojoExecutionException {
private BuildInfoWriter bi;

private void describeBuildOutput() throws MojoExecutionException {
rootPath = getExecutionRoot().getBasedir().toPath();
rootPath = session.getTopLevelProject().getBasedir().toPath();
bi = newBuildInfoWriter(null, false);

Map<String, Long> groupIds = session.getProjects().stream()
Expand Down

0 comments on commit 5b81b72

Please sign in to comment.