Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -45,6 +45,19 @@ public interface Language extends ExtensibleEnum {
*/
Language NONE = language("none");

/**
* The "resources" language. This is used for files such as images to provide in the output.
*/
Language RESOURCES = language("resources");

/**
* The "script" language. Provided for compatibility with Maven 3.
*
* @deprecated Use {@link #RESOURCES} instead.
*/
@Deprecated
Language SCRIPT = language("script");

// TODO: this should be moved out from here to Java Support (builtin into core)
Language JAVA_FAMILY = language("java");
}
111 changes: 111 additions & 0 deletions api/maven-api-core/src/main/java/org/apache/maven/api/SourceRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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;

import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.List;
import java.util.Optional;

/**
* A root directory of source files.
* The sources may be Java main classes, test classes, resources or anything else identified by the scope.
*/
public interface SourceRoot {
/**
* {@return the root directory where the sources are stored}.
* The path is relative to the <abbr>POM</abbr> file.
*/
Path directory();

/**
* {@return the list of pattern matchers for the files to include}.
* The default implementation returns an empty list, which means to apply a language-dependent pattern.
* For example, for the Java language, the pattern includes all files with the {@code .java} suffix.
*/
default List<PathMatcher> includes() {
return List.of();
}

/**
* {@return the list of pattern matchers for the files to exclude}.
* The exclusions are applied after the inclusions.
* The default implementation returns an empty list.
*/
default List<PathMatcher> excludes() {
return List.of();
}

/**
* {@return in which context the source files will be used}.
* The default value is {@link ProjectScope#MAIN}.
*/
default ProjectScope scope() {
return ProjectScope.MAIN;
}

/**
* {@return the language of the source files}.
*/
Language language();

/**
* {@return the name of the Java module (or other language-specific module) which is built by the sources}.
* The default value is empty.
*/
default Optional<String> module() {
return Optional.empty();
}

/**
* {@return the version of the platform where the code will be executed}.
* In a Java environment, this is the value of the {@code --release} compiler option.
* The default value is empty.
*/
default Optional<Version> targetVersion() {
return Optional.empty();
}

/**
* {@return an explicit target path, overriding the default value}.
* When a target path is explicitly specified, the values of the {@link #module()} and {@link #targetVersion()}
* elements are not used for inferring the path (they are still used as compiler options however).
* It means that for scripts and resources, the files below the path specified by {@link #directory()}
* are copied to the path specified by {@code targetPath()} with the exact same directory structure.
*/
default Optional<Path> targetPath() {
return Optional.empty();
}

/**
* {@return whether resources are filtered to replace tokens with parameterized values}.
* The default value is {@code false}.
*/
default boolean stringFiltering() {
return false;
}

/**
* {@return whether the directory described by this source element should be included in the build}.
* The default value is {@code true}.
*/
default boolean enabled() {
return true;
}
}
20 changes: 13 additions & 7 deletions api/maven-api-model/src/main/mdo/maven.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -775,28 +775,30 @@
<type>String</type>
</field>
<field>
<!-- TODO: replaced by <Source> with "main" scope and "resources" language -->
<name>resources</name>
<version>3.0.0+</version>
<description>
This element describes all the classpath resources such as properties
files associated with a project. These resources are often included in the final
package.
The default value is {@code src/main/resources}.

@deprecated Replaced by {@code &lt;Source&gt;} with {@code main} scope and {@code resources} language.
</description>
<association>
<type>Resource</type>
<multiplicity>*</multiplicity>
</association>
</field>
<field>
<!-- TODO: replaced by <Source> with "test" scope and "resources" language -->
<name>testResources</name>
<version>4.0.0+</version>
<description>
This element describes all the classpath resources such as properties
files associated with a project's unit tests.
The default value is {@code src/test/resources}.

@deprecated Replaced by {@code &lt;Source&gt;} with {@code test} scope and {@code resources} language.
</description>
<association>
<type>Resource</type>
Expand Down Expand Up @@ -874,7 +876,6 @@
</association>
</field>
<field>
<!-- TODO: replaced by <Source> with "main" scope. -->
<name>sourceDirectory</name>
<version>3.0.0+</version>
<required>true</required>
Expand All @@ -883,11 +884,12 @@
generated build system will compile the sources from this directory when the project is
built. The path given is relative to the project descriptor.
The default value is {@code src/main/java}.

@deprecated Replaced by {@code &lt;Source&gt;} with {@code main} scope.
</description>
<type>String</type>
</field>
<field>
<!-- TODO: replaced by <Source> with "script" language -->
<name>scriptSourceDirectory</name>
<version>4.0.0+</version>
<required>true</required>
Expand All @@ -897,11 +899,12 @@
contents will be copied to the output directory in most cases (since scripts are
interpreted rather than compiled).
The default value is {@code src/main/scripts}.

@deprecated Replaced by {@code &lt;Source&gt;} with {@code script} language.
</description>
<type>String</type>
</field>
<field>
<!-- TODO: replaced by <Source> with "test" scope. -->
<name>testSourceDirectory</name>
<version>4.0.0+</version>
<required>true</required>
Expand All @@ -910,6 +913,8 @@
project. The generated build system will compile these directories when the project is
being tested. The path given is relative to the project descriptor.
The default value is {@code src/test/java}.

@deprecated Replaced by {@code &lt;Source&gt;} with {@code test} scope.
</description>
<type>String</type>
</field>
Expand Down Expand Up @@ -2041,7 +2046,7 @@
]]>
</description>
<type>String</type>
<defaultValue>main</defaultValue>
<defaultValue>java</defaultValue>
</field>
<field>
<name>module</name>
Expand Down Expand Up @@ -2143,7 +2148,6 @@
</fields>
</class>
<class>
<!-- TODO: Replaced by <Source> with "resources" language. -->
<name>Resource</name>
<description>This element describes all of the classpath resources associated with a project
or unit tests.</description>
Expand All @@ -2161,6 +2165,8 @@
element with this value: {@code org/apache/maven/messages}.
This is not required if you simply put the resources in that directory
structure at the source, however.

@deprecated Replaced by {@code &lt;Source&gt;} with {@code resources} language.
</description>
<type>String</type>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import javax.inject.Named;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -31,13 +30,14 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.Language;
import org.apache.maven.api.ProducedArtifact;
import org.apache.maven.api.Project;
import org.apache.maven.api.ProjectScope;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.SourceRoot;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.SessionScoped;
import org.apache.maven.api.model.Resource;
Expand All @@ -48,7 +48,6 @@
import org.apache.maven.project.MavenProject;
import org.eclipse.sisu.Typed;

import static java.util.stream.Collectors.toList;
import static org.apache.maven.impl.Utils.map;
import static org.apache.maven.impl.Utils.nonNull;

Expand Down Expand Up @@ -131,30 +130,15 @@ public void attachArtifact(Project project, ProducedArtifact artifact, Path path
@Override
public List<Path> getCompileSourceRoots(Project project, ProjectScope scope) {
MavenProject prj = getMavenProject(nonNull(project, "project"));
List<String> roots;
if (nonNull(scope, "scope") == ProjectScope.MAIN) {
roots = prj.getCompileSourceRoots();
} else if (scope == ProjectScope.TEST) {
roots = prj.getTestCompileSourceRoots();
} else {
throw new IllegalArgumentException("Unsupported scope " + scope);
}
return roots.stream()
.map(Paths::get)
.collect(Collectors.collectingAndThen(toList(), Collections::unmodifiableList));
return prj.getSourceRoots(scope, Language.JAVA_FAMILY)
.map(SourceRoot::directory)
.toList();
}

@Override
public void addCompileSourceRoot(Project project, ProjectScope scope, Path sourceRoot) {
MavenProject prj = getMavenProject(nonNull(project, "project"));
String root = nonNull(sourceRoot, "sourceRoot").toAbsolutePath().toString();
if (nonNull(scope, "scope") == ProjectScope.MAIN) {
prj.addCompileSourceRoot(root);
} else if (scope == ProjectScope.TEST) {
prj.addTestCompileSourceRoot(root);
} else {
throw new IllegalArgumentException("Unsupported scope " + scope);
}
prj.addSourceRoot(nonNull(scope, "scope"), Language.JAVA_FAMILY, nonNull(sourceRoot, "sourceRoot"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

import org.apache.maven.ProjectCycleException;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.Language;
import org.apache.maven.api.ProjectScope;
import org.apache.maven.api.SessionData;
import org.apache.maven.api.model.Build;
import org.apache.maven.api.model.Dependency;
Expand Down Expand Up @@ -73,6 +75,7 @@
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.impl.DefaultSourceRoot;
import org.apache.maven.impl.InternalSession;
import org.apache.maven.impl.resolver.ArtifactDescriptorUtils;
import org.apache.maven.model.building.DefaultModelProblem;
Expand Down Expand Up @@ -576,9 +579,43 @@ private void initProject(MavenProject project, ModelBuilderResult result) {
// only set those on 2nd phase, ignore on 1st pass
if (project.getFile() != null) {
Build build = project.getBuild().getDelegate();
project.addScriptSourceRoot(build.getScriptSourceDirectory());
project.addCompileSourceRoot(build.getSourceDirectory());
project.addTestCompileSourceRoot(build.getTestSourceDirectory());
List<org.apache.maven.api.model.Source> sources = build.getSources();
Path baseDir = project.getBaseDirectory();
InternalSession s = InternalSession.from(session);
boolean hasScript = false;
boolean hasMain = false;
boolean hasTest = false;
for (var source : sources) {
var src = new DefaultSourceRoot(s, baseDir, source);
project.addSourceRoot(src);
Language language = src.language();
if (Language.JAVA_FAMILY.equals(language)) {
ProjectScope scope = src.scope();
if (ProjectScope.MAIN.equals(scope)) {
hasMain = true;
} else {
hasTest |= ProjectScope.TEST.equals(scope);
}
} else {
hasScript |= Language.SCRIPT.equals(language);
}
}
/*
* `sourceDirectory`, `testSourceDirectory` and `scriptSourceDirectory`
* are ignored if the POM file contains at least one <source> element
* for the corresponding scope and langiage. This rule exists because
* Maven provides default values for those elements which may conflict
* with user's configuration.
*/
if (!hasScript) {
project.addScriptSourceRoot(build.getScriptSourceDirectory());
}
if (!hasMain) {
project.addCompileSourceRoot(build.getSourceDirectory());
}
if (!hasTest) {
project.addTestCompileSourceRoot(build.getTestSourceDirectory());
}
}

project.setActiveProfiles(
Expand Down
Loading
Loading