【问题标题】:Migrate from individual deps to alignment rules从单个部门迁移到对齐规则
【发布时间】:2018-12-09 13:15:38
【问题描述】:

这是我的原始代码,适用于旧机制

package com.xenoterracide.gradle.plugin;

import io.spring.gradle.dependencymanagement.DependencyManagementPlugin;
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

import java.util.concurrent.TimeUnit;

import static com.xenoterracide.gradle.plugin.DepConstants.APT;
import static com.xenoterracide.gradle.plugin.DepConstants.COMPILE;
import static com.xenoterracide.gradle.plugin.DepConstants.IMPL;
import static com.xenoterracide.gradle.plugin.DepConstants.LATEST;
import static com.xenoterracide.gradle.plugin.DepConstants.TEST_COMPILE;
import static com.xenoterracide.gradle.plugin.DepConstants.TEST_IMPL;
import static com.xenoterracide.gradle.plugin.Dependencies.AssertJ;
import static com.xenoterracide.gradle.plugin.Dependencies.D;
import static com.xenoterracide.gradle.plugin.Dependencies.EqualsVerifier;

public class DependencyManagement implements Plugin<Project> {
    @Override
    public void apply( Project project ) {
        project.getConfigurations().all( conf -> {
            conf.resolutionStrategy( rs -> rs.cacheChangingModulesFor( 1, TimeUnit.MINUTES ) );
        } );
        project.getPluginManager().apply( DependencyManagementPlugin.class );
        project.getExtensions().configure( DependencyManagementExtension.class, ext -> {
            ext.imports( handler -> {
                handler.mavenBom( "org.springframework.boot:spring-boot-starter-parent:2.0.6.RELEASE" );
            } );
        } );

        project.getDependencies().constraints( dch -> {
            dch.add( TEST_IMPL, String.join( D, AssertJ.G, AssertJ.A, AssertJ.V ) );
            dch.add( TEST_IMPL, String.join( D, EqualsVerifier.G, EqualsVerifier.A, EqualsVerifier.V ) );
            dch.add( COMPILE, "org.immutables:value:2.+" );
            dch.add( IMPL, "com.google.guava:guava:" + LATEST );
            dch.add( APT, "org.immutables:value:2.+" );
            dch.add( APT, "org.immutables:builder:2.+" );
            dch.add( TEST_COMPILE, "org.immutables:value:2.+" );
        } );

这是我的new code for handling dep management

package com.xenoterracide.gradle.plugin;

import com.xenoterracide.gradle.alignmentrules.ImmutablesAlignmentRule;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.dsl.DependencyHandler;

import java.util.concurrent.TimeUnit;

import static com.xenoterracide.gradle.plugin.DepConstants.IMPL;
import static com.xenoterracide.gradle.plugin.DepConstants.LATEST;
import static com.xenoterracide.gradle.plugin.DepConstants.TEST_IMPL;
import static com.xenoterracide.gradle.plugin.Dependencies.D;
import static com.xenoterracide.gradle.plugin.Dependencies.EqualsVerifier;

public class DependencyManagement implements Plugin<Project> {
    @Override
    public void apply( Project project ) {
        project.getConfigurations().all( conf -> {
            conf.resolutionStrategy( rs -> rs.cacheChangingModulesFor( 1, TimeUnit.MINUTES ) );
        } );

        DependencyHandler deps = project.getDependencies();
        deps.getComponents().all( ImmutablesAlignmentRule.class );


    }
}

以及对齐规则

package com.xenoterracide.gradle.alignmentrules;

import org.gradle.api.artifacts.ComponentMetadataContext;
import org.gradle.api.artifacts.ComponentMetadataDetails;
import org.gradle.api.artifacts.ComponentMetadataRule;

import java.util.Objects;

public class ImmutablesAlignmentRule implements ComponentMetadataRule {
    @Override
    public void execute( ComponentMetadataContext ctx ) {
        ComponentMetadataDetails details = ctx.getDetails();
        String group = "org.immutables";
        details.allVariants( vm -> {
            if ( Objects.equals( details.getId().getGroup(), group ) ) {
                details.belongsTo( group + ":platform:2.+", true );
            }
        } );
    }
}

但测试运行显示无法解析 org.immutables:value,断点测试表明 java 从未进入对齐处理程序方法。

我需要更改哪些内容才能使不可变对象对齐?

【问题讨论】:

    标签: java gradle gradle-plugin


    【解决方案1】:

    显然虚拟对齐不能提供版本,所以要么所有依赖项都必须有版本,要么你必须为它们提供约束

    public class DependencyManagement implements Plugin<Project> {
        private final Logger log = LoggerFactory.getLogger( this.getClass() );
        private final String platform = "org.springframework.boot:spring-boot-starter-parent:2.1.+";
        private final List<String> scopes = Arrays.asList( APT, COMPILE, IMPL, TEST_COMPILE );
    
    
        private static String immutables( String artifact ) {
            return String.join( D, "org.immutables", artifact, "2.+" );
        }
    
        @Override
        public void apply( Project project ) {
            log.info( "starting plugin {} for: {}", this.getClass().getSimpleName(), project.getName() );
    
            project.getConfigurations().all( conf -> {
                conf.resolutionStrategy( rs -> rs.cacheChangingModulesFor( 1, TimeUnit.MINUTES ) );
            } );
    
            DependencyHandler deps = project.getDependencies();
            deps.components( cmh -> cmh.all( ImmutablesAlignmentRule.class ) );
            scopes.forEach( scope -> deps.add( scope, deps.enforcedPlatform( platform ) ) );
            deps.constraints( dch -> {
                scopes.forEach( scope -> {
                    Stream.of( "value", "builder" ).forEach( artifact -> dch.add( scope, immutables( artifact ) ) );
                } );
                dch.add( TEST_IMPL, String.join( D, EqualsVerifier.G, EqualsVerifier.A, EqualsVerifier.V ) );
                dch.add( IMPL, "com.google.guava:guava:" + LATEST );
            } );
        }
    }
    

    并且 aligments 应该从模块中获取版本(这似乎违反直觉)

    public class ImmutablesAlignmentRule implements ComponentMetadataRule {
        private final Logger log = LoggerFactory.getLogger( this.getClass() );
    
        @Override
        public void execute( ComponentMetadataContext ctx ) {
            ComponentMetadataDetails details = ctx.getDetails();
            ModuleVersionIdentifier module = details.getId();
            details.allVariants( vm -> {
                String group = "org.immutables";
                log.trace( "checking for immutables: {}", module );
                if ( Objects.equals( module.getGroup(), group ) ) {
                    details.belongsTo( group + ":platform:" + module.getVersion(), true );
                    log.info( "aligning immutables: {}", module );
                }
            } );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      相关资源
      最近更新 更多