Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -37,6 +38,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@Repeatable(Afters.class)
public @interface After {

/**
Expand All @@ -57,7 +59,7 @@ enum Type {
/**
* The type of this pointer.
*/
Type type();
Type type() default Type.PROJECT;

/**
* The scope for dependencies, only if {@code type() == Type.Dependencies}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.plugin.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.apache.maven.api.annotations.Experimental;

/**
* Container annotation for repeatable {@link After} annotations.
*
* @since 4.0.0
*/
@Experimental
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface Afters {

/**
* The contained {@link After} annotations.
*/
After[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.plugin.annotations;

import java.lang.annotation.Repeatable;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests the {@link After} annotation, including its {@link Repeatable} behavior.
*/
class AfterAnnotationTest {

@After(phase = "compile", type = After.Type.PROJECT)
static class SingleAfterMojo {}

@After(phase = "sources")
static class DefaultTypeMojo {}

@After(phase = "compile", type = After.Type.PROJECT)
@After(phase = "ready", type = After.Type.DEPENDENCIES, scope = "compile")
@After(phase = "package", type = After.Type.CHILDREN)
static class MultipleAfterMojo {}

@Test
void afterIsRepeatable() {
assertTrue(After.class.isAnnotationPresent(Repeatable.class));
assertEquals(Afters.class, After.class.getAnnotation(Repeatable.class).value());
}

@Test
void singleAfterAnnotation() {
After after = SingleAfterMojo.class.getAnnotation(After.class);
assertNotNull(after);
assertEquals("compile", after.phase());
assertEquals(After.Type.PROJECT, after.type());
assertEquals("", after.scope());
}

@Test
void multipleAfterAnnotations() {
After[] afters = MultipleAfterMojo.class.getAnnotationsByType(After.class);
assertNotNull(afters);
assertEquals(3, afters.length);

assertEquals("compile", afters[0].phase());
assertEquals(After.Type.PROJECT, afters[0].type());

assertEquals("ready", afters[1].phase());
assertEquals(After.Type.DEPENDENCIES, afters[1].type());
assertEquals("compile", afters[1].scope());

assertEquals("package", afters[2].phase());
assertEquals(After.Type.CHILDREN, afters[2].type());
}

@Test
void aftersContainerAnnotation() {
Afters afters = MultipleAfterMojo.class.getAnnotation(Afters.class);
assertNotNull(afters);
assertEquals(3, afters.value().length);
}

@Test
void typeDefaultsToProject() {
After after = DefaultTypeMojo.class.getAnnotation(After.class);
assertNotNull(after);
assertEquals("sources", after.phase());
assertEquals(After.Type.PROJECT, after.type());
}

@Test
void scopeDefaultsToEmpty() {
After after = SingleAfterMojo.class.getAnnotation(After.class);
assertEquals("", after.scope());
}
}
55 changes: 55 additions & 0 deletions api/maven-api-plugin/src/main/mdo/plugin.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,21 @@ under the License.
<type>String</type>
<description>the full goal name</description>
</field>
<field xdoc.separator="blank">
<name>afterLinks</name>
<version>2.0.0+</version>
<description>
Lifecycle ordering constraints declared by {@code @After} annotations on the Mojo class.
Each entry specifies that this Mojo's bound phase should execute after a given target phase,
with a pointer type ({@code PROJECT}, {@code DEPENDENCIES}, or {@code CHILDREN}) that controls
how the ordering applies across project boundaries.
@since 4.0.0
</description>
<association>
<type>AfterLink</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
</class>

Expand Down Expand Up @@ -585,6 +600,46 @@ under the License.
</fields>
</class>

<class xdoc.anchorName="afterLink">
<name>AfterLink</name>
<version>2.0.0+</version>
<description>
A lifecycle ordering constraint from an {@code @After} annotation on a Mojo class.
Specifies that the Mojo's bound phase should execute after a given target phase,
with a pointer type controlling how the ordering applies across project boundaries.
@since 4.0.0
</description>
<fields>
<field>
<name>phase</name>
<required>true</required>
<version>2.0.0+</version>
<type>String</type>
<description>The target phase name that this Mojo should run after.</description>
</field>
<field>
<name>type</name>
<required>true</required>
<version>2.0.0+</version>
<type>String</type>
<description>
The type of pointer: {@code PROJECT} (same-project phase ordering),
{@code DEPENDENCIES} (cross-project dependency ordering),
or {@code CHILDREN} (parent-child module ordering).
</description>
</field>
<field>
<name>scope</name>
<version>2.0.0+</version>
<type>String</type>
<description>
The dependency scope, only meaningful when type is {@code DEPENDENCIES}.
Examples: {@code compile}, {@code runtime}, {@code test}.
</description>
</field>
</fields>
</class>

<class xdoc.anchorName="resolution">
<name>Resolution</name>
<version>2.0.0+</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import org.apache.maven.api.Lifecycle;
import org.apache.maven.api.MonotonicClock;
import org.apache.maven.api.plugin.descriptor.AfterLink;
import org.apache.maven.api.services.LifecycleRegistry;
import org.apache.maven.api.services.MavenException;
import org.apache.maven.api.xml.XmlNode;
Expand Down Expand Up @@ -607,6 +608,8 @@ private void plan() {
.executeAfter(a));
}
}
// Apply @After annotation ordering constraints from the mojo descriptor
applyAfterLinks(mojoDescriptor, project, resolvedPhase);
});
}
}
Expand Down Expand Up @@ -634,6 +637,83 @@ private void plan() {
}
}

/**
* Applies lifecycle ordering constraints from {@code @After} annotations on a mojo descriptor.
* Each {@link AfterLink} is translated into build step ordering edges, matching the same
* semantics as {@link Lifecycle.Link} processing in {@code calculateLifecycleMappings}.
*
* @param mojoDescriptor the mojo descriptor that may contain after links
* @param project the project the mojo is bound to
* @param resolvedPhase the resolved phase the mojo is bound to
*/
private void applyAfterLinks(MojoDescriptor mojoDescriptor, MavenProject project, String resolvedPhase) {
List<AfterLink> afterLinks = mojoDescriptor.getMojoDescriptorV4().getAfterLinks();
if (afterLinks == null || afterLinks.isEmpty()) {
return;
}
for (AfterLink afterLink : afterLinks) {
String targetPhase = afterLink.getPhase();
String type = afterLink.getType();
if ("PROJECT".equals(type)) {
// Same-project ordering: this phase starts after target phase completes
plan.step(project, AFTER + targetPhase)
.ifPresent(targetAfter -> plan.requiredStep(project, BEFORE + resolvedPhase)
.executeAfter(targetAfter));
} else if ("DEPENDENCIES".equals(type)) {
// Cross-project ordering: this phase starts after each dependency's target phase completes
String scope = afterLink.getScope();
for (MavenProject dep :
filterByScope(project, plan.getAllProjects().get(project), scope)) {
plan.step(dep, AFTER + targetPhase)
.ifPresent(depAfter -> plan.requiredStep(project, BEFORE + resolvedPhase)
.executeAfter(depAfter));
}
} else if ("CHILDREN".equals(type)) {
// Parent-child ordering: bidirectional coordination with child modules
BuildStep before = plan.requiredStep(project, BEFORE + resolvedPhase);
BuildStep after = plan.requiredStep(project, AFTER + resolvedPhase);
if (project.getCollectedProjects() != null) {
project.getCollectedProjects().forEach(child -> {
plan.step(child, BEFORE + targetPhase).ifPresent(before::executeBefore);
plan.step(child, AFTER + targetPhase).ifPresent(after::executeAfter);
});
}
}
}
}

/**
* Filters upstream projects by dependency scope. If the scope is null or empty,
* all upstream projects are returned. Otherwise, only projects that the given
* project depends on with a matching scope are included.
* <p>
* Matching is exact on the dependency's declared scope string (e.g. "compile",
* "provided", "test"). Maven's default dependency scope is "compile" (when no
* scope is declared), so a null scope in the model is treated as "compile" for
* matching purposes. Note that this does <em>not</em> perform path-scope
* resolution — for example, filtering by "compile" will not include
* "provided"-scoped dependencies even though they contribute to
* {@code PathScope.MAIN_COMPILE}. This keeps the filter simple and predictable;
* broader scope-aware filtering can be added in a follow-up if needed.
*
* @param project the project whose dependencies to check
* @param upstreamProjects the list of upstream reactor projects
* @param scope the dependency scope to filter by, or null/empty for all
* @return the filtered list of upstream projects
*/
static List<MavenProject> filterByScope(
MavenProject project, List<MavenProject> upstreamProjects, String scope) {
if (scope == null || scope.isEmpty()) {
return upstreamProjects;
}
return upstreamProjects.stream()
.filter(dep -> project.getDependencies().stream()
.anyMatch(d -> dep.getGroupId().equals(d.getGroupId())
&& dep.getArtifactId().equals(d.getArtifactId())
&& scope.equals(d.getScope() != null ? d.getScope() : "compile")))
.collect(Collectors.toList());
}

protected BuildPlan computeForkPlan(BuildStep step, MojoExecution execution, BuildPlan buildPlan) {
MojoDescriptor mojoDescriptor = execution.getMojoDescriptor();
PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
Expand Down Expand Up @@ -951,8 +1031,8 @@ public BuildPlan calculateLifecycleMappings(
if (pointer instanceof Lifecycle.DependenciesPointer) {
// For dependencies: ensure current project's phase starts after dependency's phase completes
// Example: project's compile starts after dependency's package completes
// TODO: String scope = ((Lifecycle.DependenciesPointer) pointer).scope();
projects.get(project)
String scope = ((Lifecycle.DependenciesPointer) pointer).scope();
filterByScope(project, projects.get(project), scope)
.forEach(p -> plan.step(p, AFTER + n2).ifPresent(before::executeAfter));
} else if (pointer instanceof Lifecycle.ChildrenPointer) {
// For children: ensure bidirectional phase coordination
Expand Down
Loading
Loading