2020-12-24 20:58:30 +00:00
/ *
* This file is part of fabric - loom , licensed under the MIT License ( MIT ) .
*
2021-07-10 20:50:53 +00:00
* Copyright ( c ) 2020 - 2021 FabricMC
2020-12-24 20:58:30 +00:00
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the "Software" ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE .
* /
package net.fabricmc.loom.configuration ;
2021-09-05 15:08:16 +00:00
import java.lang.reflect.Method ;
2020-12-24 20:58:30 +00:00
import java.util.HashSet ;
2021-09-05 15:08:16 +00:00
import java.util.Map ;
2020-12-24 20:58:30 +00:00
import java.util.Optional ;
import java.util.Set ;
2021-09-05 15:08:16 +00:00
import java.util.concurrent.atomic.AtomicBoolean ;
2020-12-24 20:58:30 +00:00
import groovy.util.Node ;
import org.gradle.api.Project ;
import org.gradle.api.artifacts.Configuration ;
import org.gradle.api.artifacts.Dependency ;
import org.gradle.api.artifacts.ExcludeRule ;
import org.gradle.api.artifacts.ModuleDependency ;
2021-09-05 15:08:16 +00:00
import org.gradle.api.plugins.JavaPlugin ;
2020-12-24 20:58:30 +00:00
import org.gradle.api.publish.Publication ;
import org.gradle.api.publish.PublishingExtension ;
2021-09-05 15:08:16 +00:00
import net.fabricmc.loom.LoomGradleExtension ;
2020-12-24 20:58:30 +00:00
import net.fabricmc.loom.util.GroovyXmlUtil ;
public final class MavenPublication {
2021-09-05 15:08:16 +00:00
private static final Map < String , String > CONFIGURATION_TO_SCOPE = Map . of (
JavaPlugin . API_ELEMENTS_CONFIGURATION_NAME , "compile" ,
JavaPlugin . RUNTIME_ELEMENTS_CONFIGURATION_NAME , "runtime"
) ;
2020-12-24 20:58:30 +00:00
private MavenPublication ( ) {
}
public static void configure ( Project project ) {
project . afterEvaluate ( ( p ) - > {
2021-09-05 15:08:16 +00:00
AtomicBoolean reportedDeprecation = new AtomicBoolean ( false ) ;
2020-12-24 20:58:30 +00:00
2021-09-05 15:08:16 +00:00
CONFIGURATION_TO_SCOPE . forEach ( ( configurationName , scope ) - > {
Configuration config = p . getConfigurations ( ) . getByName ( configurationName ) ;
2020-12-24 20:58:30 +00:00
// add modsCompile to maven-publish
PublishingExtension mavenPublish = p . getExtensions ( ) . findByType ( PublishingExtension . class ) ;
if ( mavenPublish ! = null ) {
2021-09-05 15:08:16 +00:00
processEntry ( project , scope , config , mavenPublish , reportedDeprecation ) ;
2020-12-24 20:58:30 +00:00
}
2021-09-05 15:08:16 +00:00
} ) ;
2020-12-24 20:58:30 +00:00
} ) ;
}
2021-09-05 15:08:16 +00:00
private static boolean hasSoftwareComponent ( Publication publication ) {
try {
Method getComponent = publication . getClass ( ) . getMethod ( "getComponent" ) ;
return getComponent . invoke ( publication ) ! = null ;
} catch ( ReflectiveOperationException e ) {
// our hacks have broken!
return false ;
}
}
// TODO: Remove this in Loom 0.12
private static void processEntry ( Project project , String scope , Configuration config , PublishingExtension mavenPublish , AtomicBoolean reportedDeprecation ) {
2020-12-24 20:58:30 +00:00
mavenPublish . publications ( ( publications ) - > {
for ( Publication publication : publications ) {
2021-09-05 15:08:16 +00:00
if ( ! ( publication instanceof org . gradle . api . publish . maven . MavenPublication mavenPublication ) ) {
continue ;
}
if ( hasSoftwareComponent ( publication ) ) {
2020-12-24 20:58:30 +00:00
continue ;
2021-09-05 15:08:16 +00:00
} else if ( ! reportedDeprecation . get ( ) ) {
var deprecationHelper = LoomGradleExtension . get ( project ) . getDeprecationHelper ( ) ;
deprecationHelper . warn ( "Loom is applying dependency data manually to publications instead of using a software component (from(components[\"java\"])). This is deprecated and will be removed in Loom 0.12." ) ;
reportedDeprecation . set ( true ) ;
2020-12-24 20:58:30 +00:00
}
2021-09-05 15:08:16 +00:00
mavenPublication . pom ( ( pom ) - > pom . withXml ( ( xml ) - > {
2020-12-24 20:58:30 +00:00
Node dependencies = GroovyXmlUtil . getOrCreateNode ( xml . asNode ( ) , "dependencies" ) ;
Set < String > foundArtifacts = new HashSet < > ( ) ;
GroovyXmlUtil . childrenNodesStream ( dependencies ) . filter ( ( n ) - > "dependency" . equals ( n . name ( ) ) ) . forEach ( ( n ) - > {
Optional < Node > groupId = GroovyXmlUtil . getNode ( n , "groupId" ) ;
Optional < Node > artifactId = GroovyXmlUtil . getNode ( n , "artifactId" ) ;
if ( groupId . isPresent ( ) & & artifactId . isPresent ( ) ) {
foundArtifacts . add ( groupId . get ( ) . text ( ) + ":" + artifactId . get ( ) . text ( ) ) ;
}
} ) ;
2021-09-05 15:08:16 +00:00
for ( Dependency dependency : config . getAllDependencies ( ) ) {
2020-12-24 20:58:30 +00:00
if ( foundArtifacts . contains ( dependency . getGroup ( ) + ":" + dependency . getName ( ) ) ) {
continue ;
}
Node depNode = dependencies . appendNode ( "dependency" ) ;
depNode . appendNode ( "groupId" , dependency . getGroup ( ) ) ;
depNode . appendNode ( "artifactId" , dependency . getName ( ) ) ;
depNode . appendNode ( "version" , dependency . getVersion ( ) ) ;
2021-09-05 15:08:16 +00:00
depNode . appendNode ( "scope" , scope ) ;
2020-12-24 20:58:30 +00:00
if ( ! ( dependency instanceof ModuleDependency ) ) {
continue ;
}
final Set < ExcludeRule > exclusions = ( ( ModuleDependency ) dependency ) . getExcludeRules ( ) ;
if ( exclusions . isEmpty ( ) ) {
continue ;
}
Node exclusionsNode = depNode . appendNode ( "exclusions" ) ;
for ( ExcludeRule rule : exclusions ) {
Node exclusionNode = exclusionsNode . appendNode ( "exclusion" ) ;
exclusionNode . appendNode ( "groupId" , rule . getGroup ( ) = = null ? "*" : rule . getGroup ( ) ) ;
exclusionNode . appendNode ( "artifactId" , rule . getModule ( ) = = null ? "*" : rule . getModule ( ) ) ;
}
}
} ) ) ;
}
} ) ;
}
}