|
| 1 | +/** |
| 2 | + * This file is part of Eclipse Steady. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * SPDX-FileCopyrightText: Copyright (c) 2018-2020 SAP SE or an SAP affiliate company and Eclipse Steady contributors |
| 18 | + */ |
| 19 | +package org.eclipse.steady.java.tasks; |
| 20 | + |
| 21 | +import java.io.FileWriter; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.HashSet; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +import org.apache.logging.log4j.Logger; |
| 34 | +import org.eclipse.steady.Construct; |
| 35 | +import org.eclipse.steady.core.util.CoreConfiguration; |
| 36 | +import org.eclipse.steady.goals.GoalConfigurationException; |
| 37 | +import org.eclipse.steady.goals.GoalExecutionException; |
| 38 | +import org.eclipse.steady.java.JarAnalyzer; |
| 39 | +import org.eclipse.steady.shared.enums.GoalClient; |
| 40 | +import org.eclipse.steady.shared.enums.ProgrammingLanguage; |
| 41 | +import org.eclipse.steady.shared.util.StringList; |
| 42 | +import org.eclipse.steady.shared.util.StringUtil; |
| 43 | +import org.eclipse.steady.shared.util.VulasConfiguration; |
| 44 | +import org.eclipse.steady.tasks.AbstractTask; |
| 45 | +import org.eclipse.steady.tasks.DebloatTask; |
| 46 | +import org.vafer.jdependency.Clazz; |
| 47 | +import org.vafer.jdependency.Clazzpath; |
| 48 | +import org.vafer.jdependency.ClazzpathUnit; |
| 49 | + |
| 50 | +/** |
| 51 | + * <p>JavaBomTask class.</p> |
| 52 | + */ |
| 53 | +public class JavaDebloatTask extends AbstractTask implements DebloatTask { |
| 54 | + |
| 55 | + private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(); |
| 56 | + |
| 57 | + private static final String[] EXT_FILTER = new String[] {"jar", "war", "class", "java", "aar"}; |
| 58 | + |
| 59 | + private String[] appPrefixes = null; |
| 60 | + |
| 61 | + private StringList appJarNames = null; |
| 62 | + |
| 63 | + private static final List<GoalClient> pluginGoalClients = |
| 64 | + Arrays.asList(GoalClient.MAVEN_PLUGIN, GoalClient.GRADLE_PLUGIN); |
| 65 | + |
| 66 | + /** {@inheritDoc} */ |
| 67 | + @Override |
| 68 | + public Set<ProgrammingLanguage> getLanguage() { |
| 69 | + return new HashSet<ProgrammingLanguage>( |
| 70 | + Arrays.asList(new ProgrammingLanguage[] {ProgrammingLanguage.JAVA})); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns true if the configuration setting {@link CoreConfiguration#APP_PREFIXES} shall be considered, false otherwise. |
| 75 | + */ |
| 76 | + private final boolean useAppPrefixes() { |
| 77 | + return this.appPrefixes != null && !this.isOneOfGoalClients(pluginGoalClients); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns true if the configuration setting {@link CoreConfiguration#APP_PREFIXES} shall be considered, false otherwise. |
| 82 | + */ |
| 83 | + private final boolean useAppJarNames() { |
| 84 | + return this.appJarNames != null && !this.isOneOfGoalClients(pluginGoalClients); |
| 85 | + } |
| 86 | + |
| 87 | + /** {@inheritDoc} */ |
| 88 | + @Override |
| 89 | + public void configure(VulasConfiguration _cfg) throws GoalConfigurationException { |
| 90 | + super.configure(_cfg); |
| 91 | + |
| 92 | + // App constructs identified using package prefixes |
| 93 | + this.appPrefixes = _cfg.getStringArray(CoreConfiguration.APP_PREFIXES, null); |
| 94 | + |
| 95 | + // Print warning message in case the setting is used as part of the Maven plugin |
| 96 | + if (this.appPrefixes != null && this.isOneOfGoalClients(pluginGoalClients)) { |
| 97 | + log.warn( |
| 98 | + "Configuration setting [" |
| 99 | + + CoreConfiguration.APP_PREFIXES |
| 100 | + + "] ignored when running the goal as Maven plugin"); |
| 101 | + this.appPrefixes = null; |
| 102 | + } |
| 103 | + |
| 104 | + // App constructs identified using JAR file name patterns (regex) |
| 105 | + final String[] app_jar_names = _cfg.getStringArray(CoreConfiguration.APP_JAR_NAMES, null); |
| 106 | + if (app_jar_names != null) { |
| 107 | + // Print warning message in case the setting is used as part of the Maven plugin |
| 108 | + if (this.isOneOfGoalClients(pluginGoalClients)) { |
| 109 | + log.warn( |
| 110 | + "Configuration setting [" |
| 111 | + + CoreConfiguration.APP_JAR_NAMES |
| 112 | + + "] ignored when running the goal as Maven plugin"); |
| 113 | + this.appJarNames = null; |
| 114 | + } else { |
| 115 | + this.appJarNames = new StringList(); |
| 116 | + this.appJarNames.addAll(app_jar_names); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // CLI: Only one of appPrefixes and appJarNames can be used |
| 121 | + if (!this.isOneOfGoalClients(pluginGoalClients)) { |
| 122 | + if (this.appPrefixes != null && this.appJarNames != null) { |
| 123 | + throw new GoalConfigurationException( |
| 124 | + "Exactly one of the configuration settings [" |
| 125 | + + CoreConfiguration.APP_PREFIXES |
| 126 | + + "] and [" |
| 127 | + + CoreConfiguration.APP_JAR_NAMES |
| 128 | + + "] must be set"); |
| 129 | + } else if (this.appPrefixes == null && this.appJarNames == null) { |
| 130 | + throw new GoalConfigurationException( |
| 131 | + "Exactly one of the configuration settings [" |
| 132 | + + CoreConfiguration.APP_PREFIXES |
| 133 | + + "] and [" |
| 134 | + + CoreConfiguration.APP_JAR_NAMES |
| 135 | + + "] must be set"); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** {@inheritDoc} */ |
| 141 | + @Override |
| 142 | + public void execute() throws GoalExecutionException { |
| 143 | + |
| 144 | + final Clazzpath cp = new Clazzpath(); |
| 145 | + |
| 146 | + List<ClazzpathUnit> app = new ArrayList<ClazzpathUnit>(); |
| 147 | + try { |
| 148 | + //1) Add app paths |
| 149 | + if (this.hasSearchPath()) { |
| 150 | + for (Path p : this.getSearchPath()) { |
| 151 | + log.info( |
| 152 | + "Add app path [" |
| 153 | + + p |
| 154 | + + "] to classpath"); |
| 155 | + app.add(cp.addClazzpathUnit(p)); |
| 156 | + } |
| 157 | + } |
| 158 | + //2) Add dependencies to classpath |
| 159 | + if (this.getKnownDependencies() != null) { |
| 160 | + for (Path p : this.getKnownDependencies().keySet()) { |
| 161 | + log.info( |
| 162 | + "Add dep path [" |
| 163 | + + p |
| 164 | + + "] to classpath"); |
| 165 | + cp.addClazzpathUnit(p); |
| 166 | + } |
| 167 | + } |
| 168 | + } catch (IOException e) { |
| 169 | + // TODO Auto-generated catch block |
| 170 | + e.printStackTrace(); |
| 171 | + } |
| 172 | + |
| 173 | + final Set<Clazz> needed = new HashSet<Clazz>(); |
| 174 | + final Set<Clazz> removable = cp.getClazzes(); |
| 175 | + for(ClazzpathUnit u: app) { |
| 176 | + removable.removeAll(u.getClazzes()); |
| 177 | + removable.removeAll(u.getTransitiveDependencies()); |
| 178 | + needed.addAll(u.getClazzes()); |
| 179 | + needed.addAll(u.getTransitiveDependencies()); |
| 180 | + } |
| 181 | + |
| 182 | + log.info("Needed ["+ needed.size()+"] classes"); |
| 183 | + log.info("Removable ["+ removable.size()+"] classes"); |
| 184 | + for(Clazz clazz : removable) { |
| 185 | + System.out.println("class " + clazz + " is not required"); |
| 186 | + } |
| 187 | + try { |
| 188 | + FileWriter writer=new FileWriter("needed.txt"); |
| 189 | + for(Clazz c: needed) { |
| 190 | + writer.write(c + System.lineSeparator()); |
| 191 | + } |
| 192 | + writer.close(); |
| 193 | + } catch (IOException e){// TODO Auto-generated catch block |
| 194 | + e.printStackTrace();} |
| 195 | + } |
| 196 | + |
| 197 | +} |
0 commit comments