I want to add the Google Cloud Platform to my Java application (https://cloud.google.com/secret-manager/docs/reference/libraries). I added dependencies to my pom.xml
file as instructed:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.23.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-secretmanager</artifactId>
</dependency>
And added the module to my module-info.java
file:
requires google.cloud.secretmanager;
Issue #1: Type Not Accessible Error
When I import classes, as instructed:
import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse;
import com.google.cloud.secretmanager.v1.ProjectName;
import com.google.cloud.secretmanager.v1.Replication;
import com.google.cloud.secretmanager.v1.Secret;
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; //works
import com.google.cloud.secretmanager.v1.SecretPayload;
import com.google.cloud.secretmanager.v1.SecretVersion;
import com.google.protobuf.ByteString;
Only the import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
imports successfully, all of the other classes do not, giving me error: The type com.google.cloud.secretmanager.v1.ListSecretsRequest is not accessible)
.
Issue #2: ClassNotFoundException
As I tried to compile the code, using the VSCode debugger
'C:\Program Files\Microsoft\jdk-21.0.1.12-hotspot\bin\java.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:63218' '@C:\Users\phill\AppData\Local\Temp\cp_4rjb4t8mfd73pngxbgeub8e7.argfile' '-m' 'NTagJavaSampleApp/com.sample.ntag.Main'
It started giving me compilation errors, such as:
Caused by: java.lang.Error: Unresolved compilation problems:
The type com.google.api.gax.core.BackgroundResource cannot be resolved. It is indirectly referenced from required type com.google.cloud.secretmanager.v1.SecretManagerServiceClient
The resource type SecretManagerServiceClient does not implement java.lang.AutoCloseable
Which prompted me to install:
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
</dependency>
And add:
requires gax;
And gave the error:
java.lang.ClassNotFoundException: com.google.cloud.secretmanager.v1.ListSecretsRequest
Issue #3: Multiple Module Error
It turns out, the google.cloud.secretmanager-2.24.0.jar
file contains the com.google.cloud.secretmanager.v1.SecretManagerServiceClient
class (which is needed for the initializing the client), but not the com.google.cloud.secretmanager.v1.ListSecretsRequest
class, which also declares the package package com.google.cloud.secretmanager.v1
but is found in the proto-google-cloud-secretmanager-v1-2.24.0.jar
file.
So, I installed that (https://mvnrepository.com/artifact/com.google.api.grpc/proto-google-cloud-secretmanager-v1):
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-secretmanager-v1</artifactId>
<version>2.23.0</version>
</dependency>
And added it:
requires proto.google.cloud.secretmanager.v1;
But now, when I try to import from any class from com.google.cloud.secretmanager.v1
, (such as to initialize the client: import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
), it gives the error The package com.google.cloud.secretmanager.v1 is accessible from more than one module: google.cloud.secretmanager, proto.google.cloud.secretmanager.v1Java(8390063)
. I feel like it's trying to take me through a web of "inner dependencies", accessing deeply nested libraries that should only be called on by higher libraries. However, I'm having trouble importing the right classes, since the packages are dispersed between multiple jar files.
FYI, I went down the rabbit hole of these errors, and ended up installing:
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
<version>2.28.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>gax</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
</dependency>
<dependency> <!-- necessary for Java 9+ -->
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
<scope>provided</scope>
</dependency>
And adding:
requires com.google.api.apicommon;
requires protobuf.java;
requires grpc.protobuf;
requires grpc.stub;
requires annotations.api;
requires gax;
requires gax.grpc;
But I doubt that's the right way of doing it.