从各个部门迁移到对齐规则

时间:2018-12-09 13:15:39

标签: java gradle gradle-plugin

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

background-color:red

这是我的new code for handling dep management

<div class="a">
    <div class="b">
      <input type="text">
      <button>aa</button>
    </div>
  </div>

和对齐规则

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.+" );
        } );

但是测试运行显示无法解析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 ); } } ,断点测试表明Java从未输入对齐处理程序方法。

我需要改变什么来使不可变物对齐?

1 个答案:

答案 0 :(得分: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 );
        } );
    }
}

并且附件应该从模块中获取版本(这似乎很不直观)

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 );
            }
        } );
    }
}