others-how to solve maven Could not transfer metadata bad gateway and forbidden error?

1. Purpose

In this post, I will show you how to solve the following error when trying to build a maven task:

when running:

mvn protobuf:compile

I got this error:

[WARNING] Could not transfer metadata io.grpc:grpc-api/maven-metadata.xml from/to mvnrepository (http://www.mvnrepository.com): Access denied to: http://www.mvnrepository.com/io/grpc/grpc-api/maven-metadata.xml , ReasonPhrase:Forbidden.

[WARNING] Could not transfer metadata io.grpc:grpc-api/maven-metadata.xml from/to mvnrepository2 (http://central.maven.org/maven2/): Failed to transfer file: http://central.maven.org/maven2/io/grpc/grpc-api/maven-metadata.xml. Return code is: 502 , ReasonPhrase:Bad Gateway.

[WARNING] Could not transfer metadata io.grpc:grpc-api/maven-metadata.xml from/to jcenter (http://jcenter.bintray.com): Failed to transfer file: http://jcenter.bintray.com/io/grpc/grpc-api/maven-metadata.xml. Return code is: 502 , ReasonPhrase:Bad Gateway.

How to solve the above warning?



2. Solution

The pom.xml of my project:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>test-parent</artifactId>
        <groupId>com.test.bswen</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-grpc</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <io.grpc.version>1.40.1</io.grpc.version>
        <protoc.version>3.17.2</protoc.version>
        <os-maven-plugin.version>1.6.2</os-maven-plugin.version>
        <protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
        <annotation-api.version>1.2</annotation-api.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <scope>runtime</scope>
            <version>${io.grpc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>${io.grpc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>${io.grpc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-testing</artifactId>
            <version>${io.grpc.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>${annotation-api.version}</version>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.1</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>${protobuf-maven-plugin.version}</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${io.grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

maven version:

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /Users/bswen/tech/maven/apache-maven-3.3.9
Java version: 1.8.0_341, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_342.jdk/Contents/Home/jre
Default locale: en_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.2", arch: "x86_64", family: "mac"
[WARNING] 
[WARNING] Some problems were encountered while building the effective settings
[WARNING] 'profiles.profile[nexus-public].repositories.repository.id' must be unique but found duplicate repository with id mvnrepository @ /Users/bswen/.m2/settings.xml
[WARNING] 
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Detecting the operating system and CPU architecture
[INFO] ------------------------------------------------------------------------
[INFO] os.detected.name: osx
[INFO] os.detected.arch: x86_64
[INFO] os.detected.version: 10.15
[INFO] os.detected.version.major: 10
[INFO] os.detected.version.minor: 15
[INFO] os.detected.classifier: osx-x86_64

the settings.xml I am using:

...

    <profile>  
      <id>nexus-public</id>  
      <repositories>  
        <repository>
            <id>mvnrepository</id>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
        <repository>
           <id>sonatype</id>
           <name>Sonatype Groups</name>
           <url>https://oss.sonatype.org/content/groups/public/</url>
         </repository>
        <repository>
        <id>mvnrepository2</id>
        <url>http://central.maven.org/maven2/</url>
      </repository>
        <repository>
            <id>jcenter</id>
            <name>jcenter</name>
            <url>http://jcenter.bintray.com</url>
        </repository>
        <repository>
                <id>mvnrepository</id>
                <name>mvnrepository</name>
                <url>http://www.mvnrepository.com</url>
            </repository> 
       <repository>
	        <id>jitpack.io</id>
	        <url>https://jitpack.io</url>
	    </repository> 
        
        <repository>
            <id>javanet</id>
            <name>Repository for Maven2</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>google</id>
            <name>Google repository for Maven2</name>
            <url>https://oss.sonatype.org/content/repositories/google-releases/</url>
        </repository>
        <repository>
            <id>jboss</id>
            <name>JBoss repository for Maven2</name>
            <url>http://repository.jboss.org/maven2/</url>
        </repository>
        <repository>
            <id>com.springsource.repository.bundles.release</id>
            <name>SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases</name>
            <url>http://repository.springsource.com/maven/bundles/release</url>
        </repository>
        <repository>
            <id>com.springsource.repository.bundles.external</id>
            <name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
            <url>http://repository.springsource.com/maven/bundles/external</url>
        </repository>
        <repository>
            <id>com.springsource.repository.libraries.release</id>
            <name>SpringSource Enterprise Bundle Repository - SpringSource Library Releases</name>
            <url>http://repository.springsource.com/maven/libraries/release</url>
        </repository>
        <repository>
            <id>com.springsource.repository.libraries.external</id>
            <name>SpringSource Enterprise Bundle Repository - External Library Releases</name>
            <url>http://repository.springsource.com/maven/libraries/external</url>
        </repository>
       </repositories>
    </profile>   
  </profiles>

    <activeProfiles> 
         <activeProfile>nexus-public</activeProfile> 
    </activeProfiles>  

You can see that I am using the nexus-public profile, which points to many public maven repos like “http://repo1.maven.org/maven2” and “http://central.maven.org/maven2/”,

So I checked the above repos, I found that I can not access any of the above repos.

➜  apache-maven-3.8.8-me curl http://repo1.maven.org/maven2
501 HTTPS Required.
Use https://repo1.maven.org/maven2/
More information at https://links.sonatype.com/central/501-https-required
  %

And this:

➜  apache-maven-3.8.8-me curl http://central.maven.org/maven2/
curl: (6) Could not resolve host: central.maven.org

So I change the above maven profile to use aliyun as my default profile:

	<profile>
        <id>aliyun</id>
        <repositories>
            <repository>
                <id>aliyun1</id>
                <url>https://maven.aliyun.com/repository/public</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
        </repositories>
    </profile>

    <activeProfiles> 
         <activeProfile>aliyun1</activeProfile> 
    </activeProfiles>  

Now run the maven task again:

[INFO] ----------------------< com.test.bswen:test-grpc >-----------------------
[INFO] Building test-grpc 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from aliyun1: https://maven.aliyun.com/repository/public/io/grpc/grpc-core/maven-metadata.xml
Downloaded from aliyun1: https://maven.aliyun.com/repository/public/io/grpc/grpc-core/maven-metadata.xml (5.0 kB at 5.1 kB/s)
Downloading from aliyun1: https://maven.aliyun.com/repository/public/io/grpc/grpc-api/maven-metadata.xml
Downloaded from aliyun1: https://maven.aliyun.com/repository/public/io/grpc/grpc-api/maven-metadata.xml (3.4 kB at 11 kB/s)
[INFO] 
[INFO] --- protobuf-maven-plugin:0.6.1:compile-custom (default-cli) @ test-grpc ---
Downloading from aliyun1: https://maven.aliyun.com/repository/public/io/grpc/protoc-gen-grpc-java/1.40.1/protoc-gen-grpc-java-1.40.1.pom
Downloaded from aliyun1: https://maven.aliyun.com/repository/public/io/grpc/protoc-gen-grpc-java/1.40.1/protoc-gen-grpc-java-1.40.1.pom (0 B at 0 B/s)
Downloading from aliyun1: https://maven.aliyun.com/repository/public/io/grpc/protoc-gen-grpc-java/1.40.1/protoc-gen-grpc-java-1.40.1-osx-x86_64.exe
Downloaded from aliyun1: https://maven.aliyun.com/repository/public/io/grpc/protoc-gen-grpc-java/1.40.1/protoc-gen-grpc-java-1.40.1-osx-x86_64.exe (0 B at 0 B/s)
[INFO] Compiling 1 proto file(s) to /Users/bswen/JavaProjects/bswen_tests/test-grpc/target/generated-sources/protobuf/grpc-java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS



3. Summary

Thanks for reading up here, you can see that the problem is caused by the active profile configured in my maven settings.xml , so if you got the error as mine, you can check your maven profile and remove the repos that can not access.