1. 程式人生 > 其它 >maven使用_Maven使用GPG對檔案進行簽名加密

maven使用_Maven使用GPG對檔案進行簽名加密

技術標籤:maven使用

每次手動對Maven構件進行簽名,並將簽名部署到 Maven 倉庫中去是一種很無聊且沒有技術含量的工作。為了從這種重複性的工作中解放出來,Maven 提供了一種叫 GPG 的外掛來解決這個問題。使用者只需在 pom.xml 中做對應的配置,例如:

<project>    ...    <plugins>        ...        <plugin>            <groupId>org.apache.maven.pluginsgroupId>            <artifactId>maven-gpg-pluginartifactId>            <version>1.6version>            <executions>                <execution>                    <id>signArtifactid>                    <phase>verifyphase>                    <goals>                        <goal>signgoal>                    goals>                execution>            executions>        plugin>    plugins>project>

配置好後,使用 Mvn 命令就可以完成簽名並且釋出了。當然有個前提,那就是 GPG 需要安裝好,也就是說,能在命令列中執行 GPG 命令。

當然,在實際專案過程中,對日常的 SNAPSHOT 構件進行簽名就沒有太大意義了,而且耗費資源。那有什麼辦法可以避免這點,只在版本正式釋出的時候簽名呢?當然是可以的,在 pom 中有個 release-profile。該 profile 只有在 Maven 屬性 performRelease 為 true 的時候才會被啟用,而 release:perform 執行時,會把該屬性的值設定成 true,這個時機剛好是專案進行版本釋出的時機。所以,使用者可以在 settings.xml 或 pom 中建立如下程式碼,實現只是在釋出正式版本的時候,對正式版本進行簽名。

<profiles>    <profile>        <id> release-sign-artifactsid>        <activation>            <property>                <name>performReleasename>                <value>truevalue>            property>        activation>        <build>            <plugins>                <plugin>                    <groupId>org.apache.maven,plugins                    groupId>                    <artifactId>maven-gpg-pluginartifactId>                    <version>1.6version>                    <executions>                        <execution>                            <id>signArtifactid>                            <phase>verifyphase>                            <goals>                                <goal>signgoal>                            goals>                        execution>                    executions>                plugin>            plugins>        build>    profile>profiles>

需要注意的是,因為 Maven Release Plugin 有個漏洞,release:perform 執行過程中籤名可能會導致程序永久掛起。為了避免這種情況發生,可以在 Maven Release Plugin 中提供一個 mavenExecutorId 配置,整體樣例配置程式碼如下:

<build>    <plugins>        <plugin>            <groupId> org.apache.maven.pluginsgroupId>            <artifactId>                maven-release-plugin            artifactId>            <version>2.5.3version>            <configuration>                <tagBase>https://Noble-PC:8443/svn/MvnDemoSSM/tags/svnDemo                tagBase>                <branchBase>https://Noble-PC:8443/svn/MvnDemoSSM/                    branches/svnDemo                branchBase>                <username>nobleusername>                <password>noblepassword>                <mavenExecutorId> forked-pathmavenExecutorId>            configuration>        plugin>        <plugin>            <groupId> org.apache.maven.pluginsgroupId>            <artifactId>maven-gpg-pluginartifactId>            <version>1.6version>            <executions>                <execution>                    <id> signArtifactid>                    <phase>verifyphase>                    <goals>                        <goal>signgoal>                    goals>                execution>            executions>        plugin>    plugins>build>

到這裡自動簽名的配置就完成了。當 Maven 執行 release:perform 釋出專案版本的時候,maven-gpg-plugin 就會自動對構件進行簽名。在執行的過程中,會提示輸入私鑰的密碼。

3cd9b133bba4b30283634098cd346bb8.png