问题描述
创建一个Java Console程序,用于使用Azure Key Vault Secret。在VS Code中能正常Debug,但是通过mvn clean package打包为jar文件后,使用 java -jar target/demo-1.0-SNAPSHOT.jar 却出现 no main manifest attribute问题。
VS Code中Debug的截图:
java -jar target/demo-1.0-SNAPSHOT.jar 运行错误截图:
PS C:\LBWorkSpace\MyCode\57-KeyVault-java\demo> java -jar target/demo-1.0-SNAPSHOT.jar
no main manifest attribute, in target/demo-1.0-SNAPSHOT.jar
问题解决
问题的原因就是因为通过 mvn package打包的时候,没有添加manifest。可以通过修改 pom.xml文件中的 build 部分的内容,指定mainClass的值为当前项目中main函数所在的位置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.example.kvsecretstest.App</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
示例中main函数主体:
package com.example.kvsecretstest; import com.azure.identity.AzureAuthorityHosts; import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.security.keyvault.secrets.SecretClient; import com.azure.security.keyvault.secrets.SecretClientBuilder; import com.azure.security.keyvault.secrets.models.KeyVaultSecret; /** * Hello world! * */ public class App { public static void main(String[] args) throws InterruptedException { System.out.println("Hello World!"); String keyVaultUri = "https://xxxxxxxxxxxxx.vault.azure.cn/"; System.out.printf("key vault name = xxxxxxxxx and key vault URI = %s \n", keyVaultUri); String userIdentityID = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; SecretClient secretClientidentity = new SecretClientBuilder() .vaultUrl(keyVaultUri) .credential(new DefaultAzureCredentialBuilder().managedIdentityClientId(userIdentityID) .authorityHost(AzureAuthorityHosts.AZURE_CHINA) .build()) .buildClient(); String secretName = "ExamplePassword"; KeyVaultSecret retrievedSecret = secretClientidentity.getSecret(secretName); System.out.println("Your secret's value is '" + retrievedSecret.getValue() + "'."); System.out.println("done."); } }
修改完POM.XML文件后,先mvn clean清理所有已经生成的target文件。
然后使用新的打包命令: mvn package assembly:single, 生成一个包含所有依赖项目的jar包。
然后通过java -jar 命令运行 java -jar target\demo-1.0-SNAPSHOT-jar-with-dependencies.jar
运行成功,解决no main manifest attribute 问题!
附录一:完整版的Pom.xml文件
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.kvsecretstest</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <name>demo</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-security-keyvault-secrets</artifactId> <version>4.2.3</version> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.example.kvsecretstest.App</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> </project>
参考资料
适用于 Java 的 Azure Key Vault 机密客户端库:https://docs.azure.cn/zh-cn/key-vault/secrets/quick-create-java?tabs=azure-cli
Maven生成可以直接运行的jar包的多种方式:https://xxgblog.com/2015/08/07/maven-create-executable-jar/
[END]