View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.shade.resource;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.charset.StandardCharsets;
24  import java.util.HashMap;
25  import java.util.LinkedHashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Scanner;
29  import java.util.Set;
30  import java.util.jar.JarEntry;
31  import java.util.jar.JarOutputStream;
32  
33  import org.apache.commons.io.IOUtils;
34  import org.apache.maven.plugins.shade.relocation.Relocator;
35  
36  /**
37   * Resources transformer that relocates classes in META-INF/services and appends entries in META-INF/services resources
38   * into a single resource. For example, if there are several META-INF/services/org.apache.maven.project.ProjectBuilder
39   * resources spread across many JARs the individual entries will all be concatenated into a single
40   * META-INF/services/org.apache.maven.project.ProjectBuilder resource packaged into the resultant JAR produced by the
41   * shading process.
42   */
43  public class ServicesResourceTransformer extends AbstractCompatibilityTransformer {
44      private static final String SERVICES_PATH = "META-INF/services";
45  
46      private final Map<String, Set<String>> serviceEntries = new HashMap<>();
47  
48      private long time = Long.MIN_VALUE;
49  
50      @Override
51      public boolean canTransformResource(String resource) {
52          return resource.startsWith(SERVICES_PATH);
53      }
54  
55      @Override
56      public void processResource(String resource, InputStream is, final List<Relocator> relocators, long time)
57              throws IOException {
58          resource = resource.substring(SERVICES_PATH.length() + 1);
59          for (Relocator relocator : relocators) {
60              if (relocator.canRelocateClass(resource)) {
61                  resource = relocator.relocateClass(resource);
62                  break;
63              }
64          }
65          resource = SERVICES_PATH + '/' + resource;
66  
67          Set<String> out = serviceEntries.computeIfAbsent(resource, k -> new LinkedHashSet<>());
68  
69          Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name());
70          while (scanner.hasNextLine()) {
71              String relContent = scanner.nextLine();
72              for (Relocator relocator : relocators) {
73                  if (relocator.canRelocateClass(relContent)) {
74                      relContent = relocator.applyToSourceContent(relContent);
75                  }
76              }
77              out.add(relContent);
78          }
79  
80          if (time > this.time) {
81              this.time = time;
82          }
83      }
84  
85      @Override
86      public boolean hasTransformedResource() {
87          return !serviceEntries.isEmpty();
88      }
89  
90      @Override
91      public void modifyOutputStream(JarOutputStream jos) throws IOException {
92          for (Map.Entry<String, Set<String>> entry : serviceEntries.entrySet()) {
93              String key = entry.getKey();
94              Set<String> data = entry.getValue();
95  
96              JarEntry jarEntry = new JarEntry(key);
97              jarEntry.setTime(time);
98              jos.putNextEntry(jarEntry);
99  
100             IOUtils.writeLines(data, "\n", jos, StandardCharsets.UTF_8);
101             jos.flush();
102             data.clear();
103         }
104     }
105 }