1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import org .apache .maven .artifact .repository .MavenArtifactRepository ;
20
20
import org .gradle .api .Project ;
21
21
import org .gradle .api .attributes .Usage ;
22
+ import org .gradle .api .component .AdhocComponentWithVariants ;
23
+ import org .gradle .api .component .ConfigurationVariantDetails ;
22
24
import org .gradle .api .plugins .JavaPlugin ;
25
+ import org .gradle .api .plugins .JavaPluginConvention ;
23
26
import org .gradle .api .plugins .JavaPluginExtension ;
24
27
import org .gradle .api .publish .PublishingExtension ;
25
28
import org .gradle .api .publish .maven .MavenPom ;
49
52
* </ul>
50
53
* </ul>
51
54
*
52
- * <p/>
53
- *
54
55
* @author Andy Wilkinson
55
56
* @author Christoph Dreis
56
57
* @author Mike Smithson
@@ -80,6 +81,7 @@ private void customizeMavenPublication(MavenPublication publication, Project pro
80
81
customizePom (publication .getPom (), project );
81
82
project .getPlugins ().withType (JavaPlugin .class )
82
83
.all ((javaPlugin ) -> customizeJavaMavenPublication (publication , project ));
84
+ suppressMavenOptionalFeatureWarnings (publication );
83
85
}
84
86
85
87
private void customizePom (MavenPom pom , Project project ) {
@@ -98,12 +100,35 @@ private void customizePom(MavenPom pom, Project project) {
98
100
}
99
101
100
102
private void customizeJavaMavenPublication (MavenPublication publication , Project project ) {
103
+ addMavenOptionalFeature (project );
101
104
publication .versionMapping ((strategy ) -> strategy .usage (Usage .JAVA_API , (mappingStrategy ) -> mappingStrategy
102
105
.fromResolutionOf (JavaPlugin .RUNTIME_CLASSPATH_CONFIGURATION_NAME )));
103
106
publication .versionMapping ((strategy ) -> strategy .usage (Usage .JAVA_RUNTIME ,
104
107
(mappingStrategy ) -> mappingStrategy .fromResolutionResult ()));
105
108
}
106
109
110
+ /**
111
+ * Add a feature that allows maven plugins to declare optional dependencies that
112
+ * appear in the POM. This is required to make m2e in Eclipse happy.
113
+ * @param project the project to add the feature to
114
+ */
115
+ private void addMavenOptionalFeature (Project project ) {
116
+ JavaPluginExtension extension = project .getExtensions ().getByType (JavaPluginExtension .class );
117
+ JavaPluginConvention convention = project .getConvention ().getPlugin (JavaPluginConvention .class );
118
+ extension .registerFeature ("mavenOptional" ,
119
+ (feature ) -> feature .usingSourceSet (convention .getSourceSets ().getByName ("main" )));
120
+ AdhocComponentWithVariants javaComponent = (AdhocComponentWithVariants ) project .getComponents ()
121
+ .findByName ("java" );
122
+ javaComponent .addVariantsFromConfiguration (
123
+ project .getConfigurations ().findByName ("mavenOptionalRuntimeElements" ),
124
+ ConfigurationVariantDetails ::mapToOptional );
125
+ }
126
+
127
+ private void suppressMavenOptionalFeatureWarnings (MavenPublication publication ) {
128
+ publication .suppressPomMetadataWarningsFor ("mavenOptionalApiElements" );
129
+ publication .suppressPomMetadataWarningsFor ("mavenOptionalRuntimeElements" );
130
+ }
131
+
107
132
private void customizeOrganization (MavenPomOrganization organization ) {
108
133
organization .getName ().set ("Pivotal Software, Inc." );
109
134
organization .getUrl ().set ("https://spring.io" );
0 commit comments