50
50
import java .util .Set ;
51
51
import java .util .stream .Collectors ;
52
52
53
+ import static com .alipay .sofa .ark .boot .mojo .MavenUtils .getArtifactIdentity ;
54
+ import static com .alipay .sofa .ark .boot .mojo .MavenUtils .getArtifactIdentityWithoutVersion ;
55
+ import static com .alipay .sofa .ark .boot .mojo .MavenUtils .getDependencyIdentity ;
56
+ import static com .alipay .sofa .ark .boot .mojo .MavenUtils .getGAVIdentity ;
53
57
import static com .alipay .sofa .ark .boot .mojo .MavenUtils .inUnLogScopes ;
54
58
import static com .alipay .sofa .ark .boot .mojo .utils .ParseUtils .getBooleanWithDefault ;
55
59
import static com .alipay .sofa .ark .boot .mojo .utils .ParseUtils .getStringSet ;
@@ -77,9 +81,13 @@ public class ModuleSlimStrategy {
77
81
78
82
private File baseDir ;
79
83
80
- private static final String EXTENSION_EXCLUDE_WITH_INDIRECT_DEPENDENCIES = "excludeWithIndirectDependencies" ;
84
+ private static final String EXTENSION_EXCLUDE_WITH_INDIRECT_DEPENDENCIES = "excludeWithIndirectDependencies" ;
81
85
82
- private static final String DEFAULT_EXCLUDE_RULES = "rules.txt" ;
86
+ private static final String EXTENSION_EXCLUDE_SAME_BASE_DEPENDENCY = "excludeSameBaseDependency" ;
87
+
88
+ private static final String EXTENSION_BUILD_FAIL_WHEN_EXCLUDE_DIFF_BASE_DEPENDENCY = "buildFailWhenExcludeDiffBaseDependency" ;
89
+
90
+ private static final String DEFAULT_EXCLUDE_RULES = "rules.txt" ;
83
91
84
92
ModuleSlimStrategy (MavenProject project , DependencyNode projDependencyGraph ,
85
93
ModuleSlimConfig config , File baseDir , Log log ) {
@@ -92,43 +100,121 @@ public class ModuleSlimStrategy {
92
100
93
101
public Set <Artifact > getSlimmedArtifacts () throws MojoExecutionException , IOException {
94
102
initSlimStrategyConfig ();
95
- Set <Artifact > toFilterByBase = getArtifactsToFilterByParentIdentity (project .getArtifacts ());
96
103
104
+ Set <Artifact > toFilterByBase = getArtifactsToFilterByParentIdentity (project .getArtifacts ());
97
105
Set <Artifact > toFilterByExclude = getArtifactsToFilterByExcludeConfig (project
98
106
.getArtifacts ());
99
107
Set <Artifact > toAddByInclude = getArtifactsToAddByIncludeConfig (project .getArtifacts ());
100
108
109
+ checkExcludeByParentIdentity (toFilterByExclude , toAddByInclude );
110
+
101
111
Set <Artifact > filteredArtifacts = new HashSet <>(project .getArtifacts ());
102
112
filteredArtifacts .removeAll (toFilterByBase );
103
113
filteredArtifacts .removeAll (toFilterByExclude );
104
114
filteredArtifacts .addAll (toAddByInclude );
105
115
return filteredArtifacts ;
106
116
}
107
117
118
+ protected void checkExcludeByParentIdentity (Set <Artifact > toFilterByExclude , Set <Artifact > toAddByInclude ) throws MojoExecutionException {
119
+ if (StringUtils .isEmpty (config .getBaseDependencyParentIdentity ())) {
120
+ return ;
121
+ }
122
+
123
+ Set <Artifact > toFilter = new HashSet <>(toFilterByExclude );
124
+ toFilter .removeAll (toAddByInclude );
125
+
126
+ Set <Artifact > excludedButNoDependencyInBase = getExcludedButNoDependencyInBase (toFilter );
127
+ Set <Artifact > excludedButDifferentVersionDependencyInBase = getExcludedButDifferentVersionDependencyInBase (toFilter );
128
+
129
+ if (excludedButNoDependencyInBase .isEmpty () && excludedButDifferentVersionDependencyInBase .isEmpty ()){
130
+ getLog ().info (String .format ("check excludeWithBaseDependencyParentIdentity success with base: %s" ,config .getBaseDependencyParentIdentity ()));
131
+ return ;
132
+ }
133
+
134
+ // Dependency not found in base; please add it to the base or do not exclude it in the module
135
+ excludedButNoDependencyInBase .forEach (artifact -> {
136
+ getLog ().error (
137
+ String .format (
138
+ "error to exclude package jar: %s because no such jar in base, please keep the jar or add it to base" ,
139
+ getArtifactIdentity (artifact )));
140
+ });
141
+
142
+ if (!excludedButNoDependencyInBase .isEmpty ()){
143
+ throw new MojoExecutionException (String .format ("check excludeWithBaseDependencyParentIdentity failed with base: %s" ,config .getBaseDependencyParentIdentity ()));
144
+ }
145
+
146
+ // The base contains this dependency, but the version and module are inconsistent; Please use the same dependency version as the base in the module.
147
+ List <Dependency > baseDependencies = getAllBaseDependencies ();
148
+ Map <String ,Dependency > baseDependencyIdentityWithoutVersion = baseDependencies .stream ().collect (Collectors .toMap (MavenUtils ::getDependencyIdentityWithoutVersion , it -> it ));
149
+ excludedButDifferentVersionDependencyInBase .forEach (artifact -> {
150
+ Dependency baseDependency = baseDependencyIdentityWithoutVersion .get (getArtifactIdentityWithoutVersion (artifact ));
151
+ getLog ().error (
152
+ String .format (
153
+ "error to exclude package jar: %s because it has different version with: %s in base, please keep the jar or set same version with base" ,
154
+ getArtifactIdentity (artifact ), getDependencyIdentity (baseDependency )));
155
+ });
156
+
157
+ if (config .isBuildFailWhenExcludeBaseDependencyWithDiffVersion ()){
158
+ throw new MojoExecutionException (String .format ("check excludeWithBaseDependencyParentIdentity failed with base: %s" ,config .getBaseDependencyParentIdentity ()));
159
+ }
160
+ }
161
+
108
162
protected Set <Artifact > getArtifactsToFilterByParentIdentity (Set <Artifact > artifacts )
109
163
throws MojoExecutionException {
110
164
if (StringUtils .isEmpty (config .getBaseDependencyParentIdentity ())) {
111
165
return Collections .emptySet ();
112
166
}
113
167
168
+ if (!config .isExcludeSameBaseDependency ()) {
169
+ return Collections .emptySet ();
170
+ }
171
+
114
172
// 过滤出模块和基座版本一致的依赖,即需要瘦身的依赖
115
173
return getSameVersionArtifactsWithBase (artifacts );
116
174
}
117
175
118
176
private Set <Artifact > getSameVersionArtifactsWithBase (Set <Artifact > artifacts ) throws MojoExecutionException {
177
+ List <Dependency > baseDependencies = getAllBaseDependencies ();
178
+
179
+ Set <String > dependencyIdentities = baseDependencies .stream ().map (MavenUtils ::getDependencyIdentity ).collect (Collectors .toSet ());
180
+
181
+ return artifacts .stream ().filter (it -> dependencyIdentities .contains (getArtifactIdentity (it ))).collect (Collectors .toSet ());
182
+ }
183
+
184
+ private Set <Artifact > getExcludedButNoDependencyInBase (Set <Artifact > toFilter ) throws MojoExecutionException {
185
+ List <Dependency > baseDependencies = getAllBaseDependencies ();
186
+
187
+ Map <String ,Dependency > baseDependencyIdentityWithoutVersion = baseDependencies .stream ().collect (Collectors .toMap (MavenUtils ::getDependencyIdentityWithoutVersion , it -> it ));
188
+
189
+ return toFilter .stream ().filter (it -> !baseDependencyIdentityWithoutVersion .containsKey (getArtifactIdentityWithoutVersion (it ))).collect (Collectors .toSet ());
190
+ }
191
+
192
+ private Set <Artifact > getExcludedButDifferentVersionDependencyInBase (Set <Artifact > toFilter ) throws MojoExecutionException {
193
+ List <Dependency > baseDependencies = getAllBaseDependencies ();
194
+ Map <String ,Dependency > baseDependencyIdentityWithoutVersion = baseDependencies .stream ().collect (Collectors .toMap (MavenUtils ::getDependencyIdentityWithoutVersion , it -> it ));
195
+ return toFilter .stream ().filter (artifact ->
196
+ {
197
+ String identityWithoutVersion = getArtifactIdentityWithoutVersion (artifact );
198
+ return baseDependencyIdentityWithoutVersion .containsKey (identityWithoutVersion )
199
+ && (!artifact .getBaseVersion ().equals (baseDependencyIdentityWithoutVersion .get (identityWithoutVersion ).getVersion ()));
200
+ }
201
+ ).collect (Collectors .toSet ());
202
+ }
203
+
204
+ private List <Dependency > getAllBaseDependencies () throws MojoExecutionException {
119
205
// 获取基座DependencyParent的原始Model
120
206
Model baseDependencyPom = getBaseDependencyParentOriginalModel ();
121
- if (null == baseDependencyPom ){
122
- throw new MojoExecutionException (String .format ("can not find base dependency parent: %s" ,config .getBaseDependencyParentIdentity ()));
207
+ if (null == baseDependencyPom ) {
208
+ throw new MojoExecutionException (
209
+ String .format ("can not find base dependency parent: %s" ,
210
+ config .getBaseDependencyParentIdentity ()));
123
211
}
124
212
125
- if (null == baseDependencyPom .getDependencyManagement ()){
126
- return Collections .emptySet ();
213
+ if (null == baseDependencyPom .getDependencyManagement ()) {
214
+ return Collections .emptyList ();
127
215
}
128
216
129
- List <Dependency > baseDependencies = baseDependencyPom .getDependencyManagement ().getDependencies ();
130
- Set <String > dependencyIdentities = baseDependencies .stream ().map (this ::getDependencyIdentity ).collect (Collectors .toSet ());
131
- return artifacts .stream ().filter (it -> dependencyIdentities .contains (getArtifactIdentity (it ))).collect (Collectors .toSet ());
217
+ return baseDependencyPom .getDependencyManagement ().getDependencies ();
132
218
}
133
219
134
220
protected Model getBaseDependencyParentOriginalModel () {
@@ -179,11 +265,22 @@ private String getDependencyIdentity(Dependency dependency) {
179
265
protected void initSlimStrategyConfig () throws IOException {
180
266
Map <String , Object > arkYaml = ArkConfigHolder .getArkYaml (baseDir .getAbsolutePath ());
181
267
Properties prop = ArkConfigHolder .getArkProperties (baseDir .getAbsolutePath ());
268
+
182
269
config .setExcludeWithIndirectDependencies (getBooleanWithDefault (prop ,
183
270
EXTENSION_EXCLUDE_WITH_INDIRECT_DEPENDENCIES , true ));
184
271
config .setExcludeWithIndirectDependencies (getBooleanWithDefault (arkYaml ,
185
272
EXTENSION_EXCLUDE_WITH_INDIRECT_DEPENDENCIES , true ));
186
273
274
+ config .setExcludeSameBaseDependency (getBooleanWithDefault (prop ,
275
+ EXTENSION_EXCLUDE_SAME_BASE_DEPENDENCY , true ));
276
+ config .setExcludeSameBaseDependency (getBooleanWithDefault (arkYaml ,
277
+ EXTENSION_EXCLUDE_SAME_BASE_DEPENDENCY , true ));
278
+
279
+ config .setBuildFailWhenExcludeBaseDependencyWithDiffVersion (getBooleanWithDefault (prop ,
280
+ EXTENSION_BUILD_FAIL_WHEN_EXCLUDE_DIFF_BASE_DEPENDENCY , false ));
281
+ config .setBuildFailWhenExcludeBaseDependencyWithDiffVersion (getBooleanWithDefault (arkYaml ,
282
+ EXTENSION_BUILD_FAIL_WHEN_EXCLUDE_DIFF_BASE_DEPENDENCY , false ));
283
+
187
284
initExcludeAndIncludeConfig ();
188
285
}
189
286
@@ -222,8 +319,8 @@ protected Set<Artifact> getArtifactsToFilterByExcludeConfig(Set<Artifact> artifa
222
319
}
223
320
224
321
private Set <Artifact > excludeWithIndirectDependencies (Set <Artifact > literalArtifactsToExclude , Set <Artifact > artifacts ) {
225
- Set <String > excludeArtifactIdentities = literalArtifactsToExclude .stream ().map (this ::getArtifactIdentity ).collect (Collectors .toSet ());
226
- Map <String ,Artifact > artifactMap = artifacts .stream ().collect (Collectors .toMap (this ::getArtifactIdentity ,it ->it ));
322
+ Set <String > excludeArtifactIdentities = literalArtifactsToExclude .stream ().map (MavenUtils ::getArtifactIdentity ).collect (Collectors .toSet ());
323
+ Map <String ,Artifact > artifactMap = artifacts .stream ().collect (Collectors .toMap (MavenUtils ::getArtifactIdentity ,it ->it ));
227
324
return getExcludeWithIndirectDependencies (projDependencyGraph ,excludeArtifactIdentities ,artifactMap );
228
325
}
229
326
0 commit comments