解决Gradle Publish到MavenLocal地址错误问题

gradle版本:4.1

maven版本:3.5.0

 

现象:

执行./gradlew publishMavenPublicationToMavenLocalRepository或publishMavenPublicationToMavenLocal2Repository

任务想发布到~/.m2下面,结果publish到工程当前目录的~/.m2下面;而不是到系统的~/.m2下面;从而导致其他工程找不到publish到本地的版本;

 

解决方法:

修改系统的~/.m2/settings.xml(注意不是maven安装目录下的conf/settings.xml)的localRepository配置:

<localRepository>~/.m2/repository</localRepository>

改为

<localRepository>${user.home}/.m2/repository</localRepository>

 

分析解决过程:

跟踪gradle源码,分析MavenLocal的配置过程发现:

在DefaultBaseRepositoryFactory中:

public MavenArtifactRepository createMavenLocalRepository() {
        MavenArtifactRepository mavenRepository = (MavenArtifactRepository)this.instantiator.newInstance(DefaultMavenLocalArtifactRepository.class, new Object[]{this.fileResolver, this.transportFactory, this.locallyAvailableResourceFinder, this.instantiator, this.artifactFileStore, this.pomParser, this.createAuthenticationContainer(), this.moduleIdentifierFactory, this.fileResourceRepository});
        File localMavenRepository = this.localMavenRepositoryLocator.getLocalMavenRepository();
        mavenRepository.setUrl(localMavenRepository);
        return mavenRepository;
    }

说明MavenLocal的Repository地址由localMavenRepositoryLocator.getLocalMavenRepository()生成,而localMavenRepositoryLocator实际上是DefaultLocalMavenRepositoryLocator;

查看其源码:

    public File getLocalMavenRepository() throws CannotLocateLocalMavenRepositoryException {
        String localOverride = this.system.getProperty("maven.repo.local");
        if (localOverride != null) {
            return new File(localOverride);
        } else {
            try {
                String repoPath = this.parseLocalRepoPathFromMavenSettings();
                if (repoPath != null) {
                    return new File(this.resolvePlaceholders(repoPath.trim()));
                } else {
                    File defaultLocation = (new File(this.system.getProperty("user.home"), "/.m2/repository")).getAbsoluteFile();
                    LOGGER.debug("No local repository in Settings file defined. Using default path: {}", defaultLocation);
                    return defaultLocation;
                }
            } catch (SettingsBuildingException var4) {
                throw new CannotLocateLocalMavenRepositoryException("Unable to parse local Maven settings.", var4);
            }
        }
    }

而配置路径由parseLocalRepoPathFromMavenSettings生成:

    private synchronized String parseLocalRepoPathFromMavenSettings() throws SettingsBuildingException {
        if (this.localRepoPathFromMavenSettings == null) {
            this.localRepoPathFromMavenSettings = this.settingsProvider.getLocalRepository();
        }

        return this.localRepoPathFromMavenSettings;
    }

而settingsProvider实际上是DefaultMavenSettingsProvider:

    public String getLocalRepository() {
        String localRepo = this.readLocalRepository(this.mavenFileLocations.getUserSettingsFile());
        if (localRepo == null) {
            localRepo = this.readLocalRepository(this.mavenFileLocations.getGlobalSettingsFile());
        }

        return localRepo;
    }

而mavenFileLocations实际上是DefaultMavenFileLocations:

public class DefaultMavenFileLocations implements MavenFileLocations {
    public DefaultMavenFileLocations() {
    }

    public File getUserMavenDir() {
        return new File(SystemProperties.getInstance().getUserHome(), ".m2");
    }

    @Nullable
    public File getGlobalMavenDir() {
        String m2Home = System.getenv("M2_HOME");
        return m2Home == null ? null : new File(m2Home);
    }

    public File getUserSettingsFile() {
        return new File(this.getUserMavenDir(), "settings.xml");
    }

    @Nullable
    public File getGlobalSettingsFile() {
        File dir = this.getGlobalMavenDir();
        return dir == null ? null : new File(dir, "conf/settings.xml");
    }
}

这里发现其对应配置就是~/.m2/settings.xml,对比maven安装目录下conf/settings.xml这个文件,发现用户目录不是用的~而是${user.home},因此怀疑这里的写法存在兼容性问题,因此尝试修改最终解决。

 

posted on 2020-04-26 19:30  albert1017  阅读(4020)  评论(0编辑  收藏  举报

导航