Skip to content
Closed
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 @@ -18,16 +18,11 @@
*/
package org.apache.maven.api.services;

import java.io.IOException;
import java.lang.module.ModuleDescriptor;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.api.Dependency;
import org.apache.maven.api.DependencyScope;
import org.apache.maven.api.JavaPathType;
import org.apache.maven.api.Node;
import org.apache.maven.api.PathType;
import org.apache.maven.api.annotations.Experimental;
Expand Down Expand Up @@ -102,45 +97,4 @@ public interface DependencyResolverResult extends Result<DependencyResolverReque
*/
@Nonnull
Map<Dependency, Path> getDependencies();

/**
* Returns the Java module name of the dependency at the given path.
* The given dependency should be one of the paths returned by {@link #getDependencies()}.
* The module name is extracted from the {@code module-info.class} file if present, otherwise from
* the {@code "Automatic-Module-Name"} attribute of the {@code META-INF/MANIFEST.MF} file if present.
*
* <p>A typical usage is to invoke this method for all dependencies having a
* {@link DependencyScope#TEST TEST} or {@link DependencyScope#TEST_ONLY TEST_ONLY}
* {@linkplain Dependency#getScope() scope}. An {@code --add-reads} option may need
* to be generated for compiling and running the test classes that use such dependencies.</p>
*
* @param dependency path to the dependency for which to get the module name
* @return module name of the dependency at the given path, or empty if the dependency is not modular
* @throws IOException if the module information of the specified dependency cannot be read
*/
Optional<String> getModuleName(@Nonnull Path dependency) throws IOException;

/**
* Returns the Java module descriptor of the dependency at the given path.
* The given dependency should be one of the paths returned by {@link #getDependencies()}.
* The module descriptor is extracted from the {@code module-info.class} file if present.
*
* <p>{@link #getModuleName(Path)} is preferred when only the module name is desired,
* because a name may be present even if the descriptor is absent. This method is for
* cases when more information is desired, such as the set of exported packages.</p>
*
* @param dependency path to the dependency for which to get the module name
* @return module name of the dependency at the given path, or empty if the dependency is not modular
* @throws IOException if the module information of the specified dependency cannot be read
*/
Optional<ModuleDescriptor> getModuleDescriptor(@Nonnull Path dependency) throws IOException;

/**
* If the module path contains at least one filename-based auto-module, prepares a warning message.
* The module path is the collection of dependencies associated with {@link JavaPathType#MODULES}.
* It is caller's responsibility to send the message to a logger.
*
* @return warning message if at least one filename-based auto-module was found
*/
Optional<String> warningForFilenameBasedAutomodules();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

import org.apache.maven.api.Dependency;
import org.apache.maven.api.JavaPathType;
import org.apache.maven.api.Node;
import org.apache.maven.api.PathType;
import org.apache.maven.api.services.DependencyResolverException;
import org.apache.maven.api.services.DependencyResolverRequest;
import org.apache.maven.api.services.DependencyResolverResult;

Expand Down Expand Up @@ -378,18 +376,6 @@ public Map<Dependency, Path> getDependencies() {
return dependencies;
}

@Override
public Optional<ModuleDescriptor> getModuleDescriptor(Path dependency) throws IOException {
Object value = cache.getModuleInfo(dependency).descriptors.get(dependency);
return (value instanceof ModuleDescriptor moduleDescriptor) ? Optional.of(moduleDescriptor) : Optional.empty();
}

@Override
public Optional<String> getModuleName(Path dependency) throws IOException {
return Optional.ofNullable(
name(cache.getModuleInfo(dependency).descriptors.get(dependency)));
}

/**
* Returns the module name for the given value of the {@link PathModularization#descriptors} map.
*/
Expand All @@ -402,13 +388,4 @@ private static String name(final Object value) {
return null;
}
}

@Override
public Optional<String> warningForFilenameBasedAutomodules() {
try {
return cache.warningForFilenameBasedAutomodules(dispatchedPaths.get(JavaPathType.MODULES));
} catch (IOException e) {
throw new DependencyResolverException("Cannot read module information.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.Predicate;

import org.apache.maven.api.JavaPathType;
Expand Down Expand Up @@ -158,32 +155,4 @@ Optional<PathType> selectPathType(Set<PathType> types, Predicate<PathType> filte
}
return Optional.ofNullable(selected);
}

/**
* If the module-path contains a filename-based auto-module, prepares a warning message.
* It is caller's responsibility to send the message to a logger.
*
* @param modulePaths content of the module path, or {@code null} if none
* @return warning message if at least one filename-based auto-module was found
* @throws IOException if an error occurred while reading module information
*/
Optional<String> warningForFilenameBasedAutomodules(Collection<Path> modulePaths) throws IOException {
if (modulePaths == null) {
return Optional.empty();
}
var automodulesDetected = new ArrayList<String>();
for (Path p : modulePaths) {
getModuleInfo(p).addIfFilenameBasedAutomodules(automodulesDetected);
}
if (automodulesDetected.isEmpty()) {
return Optional.empty();
}
String lineSeparator = System.lineSeparator();
var joiner = new StringJoiner(
lineSeparator + " - ",
"Filename-based automodules detected on the module path: " + lineSeparator + " - ",
lineSeparator + "Please don't publish this project to a public artifact repository.");
automodulesDetected.forEach(joiner::add);
return Optional.of(joiner.toString());
}
}