我为它创建了一个Docker映像,它无法运行,但它在Eclipse中对我有效。
我在JSON密钥中读到如下内容:
InputStream inputStream = GoogleNlpApplication.class.getClassLoader().getSystemResourceAsStream("AvayaNlp-e422dbca92e5.json");
credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(inputStream));
<profiles>
<profile>
<id>docker-build</id>
<activation>
<property>
<name>build</name>
<value>docker</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<imageName>googlenlp2vi</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<!-- copy the service's jar file from target into the root directory
of the image -->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>default</id>
<!-- phase>install</phase -->
<phase>package</phase>
<goals>
<goal>build</goal>
<!-- goal>push</goal -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>googlenlp2vi</finalName>
</build>
</profile>
</profiles>
应用程序的目录:
然后在Docker工具箱中运行:
1.mvn清洁安装
2.mvn clean package-Dbuild=docker
我得到的错误是:
更新解决方案:
我在资源中创建了一个新目录:
src
|
main
|
resources
|
test
|
AvayaNlp-e422dbca92e5.json
然后使用:
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:test/*.json");
InputStream inputStream = null;
for (Resource r : resources) {
inputStream = r.getInputStream();
}
credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(inputStream));
现在docker映像可以访问jar中的json键了,不过如果资源中的文件夹中有多个json键,它将获取所有json键。