|
| 1 | +package org.eclipse.jetty.toolchain.modifysources; |
| 2 | + |
| 3 | +import com.google.inject.Singleton; |
| 4 | +import org.apache.commons.text.StringSubstitutor; |
| 5 | +import org.apache.commons.text.lookup.StringLookup; |
| 6 | +import org.apache.maven.model.Dependency; |
| 7 | +import org.apache.maven.project.MavenProject; |
| 8 | +import org.apache.maven.shared.filtering.DefaultMavenFileFilter; |
| 9 | +import org.apache.maven.shared.filtering.DefaultMavenResourcesFiltering; |
| 10 | +import org.apache.maven.shared.filtering.FilterWrapper; |
| 11 | +import org.apache.maven.shared.filtering.MavenFileFilter; |
| 12 | +import org.apache.maven.shared.filtering.MavenFilteringException; |
| 13 | +import org.apache.maven.shared.filtering.MavenResourcesExecution; |
| 14 | +import org.apache.maven.shared.filtering.MavenResourcesFiltering; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | +import org.sonatype.plexus.build.incremental.BuildContext; |
| 18 | + |
| 19 | +import javax.inject.Inject; |
| 20 | +import javax.inject.Named; |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Optional; |
| 28 | + |
| 29 | +@Singleton |
| 30 | +@Named("mod-files") |
| 31 | +public class ModFilesFiltering extends DefaultMavenResourcesFiltering implements MavenResourcesFiltering { |
| 32 | + |
| 33 | + private static final Logger LOGGER = LoggerFactory.getLogger( DefaultMavenResourcesFiltering.class ); |
| 34 | + |
| 35 | + private static ThreadLocal<MavenProject> CURRENT_PROJECT = new ThreadLocal<>(); |
| 36 | + |
| 37 | + @Override |
| 38 | + public List<String> getDefaultNonFilteredFileExtensions() { |
| 39 | + return Collections.emptyList(); |
| 40 | + } |
| 41 | + |
| 42 | + @Inject |
| 43 | + public ModFilesFiltering(MavenFileFilter mavenFileFilter, BuildContext buildContext) { |
| 44 | + super(new JettyModFilesFileFilter(buildContext), buildContext); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void filterResources(MavenResourcesExecution mavenResourcesExecution) throws MavenFilteringException { |
| 49 | + CURRENT_PROJECT.set(mavenResourcesExecution.getMavenProject()); |
| 50 | + super.filterResources(mavenResourcesExecution); |
| 51 | + } |
| 52 | + |
| 53 | + public static class JettyModFilesFileFilter extends DefaultMavenFileFilter implements MavenFileFilter { |
| 54 | + |
| 55 | + private BuildContext buildContext; |
| 56 | + |
| 57 | + @Inject |
| 58 | + public JettyModFilesFileFilter(BuildContext buildContext) { |
| 59 | + super(buildContext); |
| 60 | + this.buildContext = buildContext; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void copyFile(File from, final File to, boolean filtering, List<FilterWrapper> filterWrappers, |
| 65 | + String encoding, boolean overwrite ) |
| 66 | + throws MavenFilteringException { |
| 67 | + |
| 68 | + MavenProject mavenProject = CURRENT_PROJECT.get(); |
| 69 | + |
| 70 | + try { |
| 71 | + // not looking at non filtered files |
| 72 | + if (filtering && Files.exists(from.toPath())) { |
| 73 | + // well it is definitely not the best option to read the full content but shouldn't be too big files |
| 74 | + String content = Files.readString(from.toPath()); |
| 75 | + StringLookup stringLookup = s -> { |
| 76 | + if(s.contains(":")) { |
| 77 | + // we have groupId:artifactId so let's get the version |
| 78 | + String[] parts = s.split(":"); |
| 79 | + String groupId = parts[0]; |
| 80 | + String artifactId = parts[1]; |
| 81 | + Optional<Dependency> dependency = findDependency(groupId, artifactId, mavenProject.getDependencies()); |
| 82 | + if (dependency.isPresent()) { |
| 83 | + return dependencyToJarName(dependency.get()); |
| 84 | + } |
| 85 | + dependency = findDependency(groupId, artifactId, mavenProject.getDependencyManagement().getDependencies()); |
| 86 | + return dependencyToJarName(dependency.orElseThrow(() -> new NullPointerException("cannot find dependency " + groupId + ":" + artifactId))); |
| 87 | + } |
| 88 | + return mavenProject.getProperties().getProperty(s); |
| 89 | + }; |
| 90 | + StringSubstitutor stringSubstitutor = new StringSubstitutor(stringLookup , "@", "@", '\\'); |
| 91 | + Files.writeString(to.toPath(), stringSubstitutor.replace(content), StandardCharsets.UTF_8); |
| 92 | + buildContext.refresh( to ); |
| 93 | + } |
| 94 | + } catch (IOException e) { |
| 95 | + LOGGER.error("error copying file {} to {}", from, to); |
| 96 | + throw new MavenFilteringException(e.getMessage(), e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + private static Optional<Dependency> findDependency(String groupId, String artifactId, List<Dependency> dependencies) { |
| 103 | + return dependencies.stream() |
| 104 | + .filter(dependency -> groupId.equals(dependency.getGroupId()) && artifactId.equals(dependency.getArtifactId())).findFirst(); |
| 105 | + } |
| 106 | + |
| 107 | + private static String dependencyToJarName(Dependency dependency) { |
| 108 | + return dependency.getArtifactId() + "-" + dependency.getVersion() + ".jar"; |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments