You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整Maven插件顺序:让exec-maven-plugin在assembly后nsis前执行

解决方案

要实现你期望的执行顺序,核心是调整插件的阶段绑定和POM中的插件声明顺序,具体步骤如下:

1. 调整assembly插件的执行阶段

将maven-assembly-plugin的执行阶段从package改为prepare-package,确保在package阶段开始前就完成所有文件的部署,这样Launcher.exe会提前出现在target目录中,避免签名时找不到文件。

2. 控制同阶段插件的执行顺序

Maven在同一个阶段内的插件执行顺序严格遵循POM中的插件声明顺序。因此:

  • 先声明exec-maven-plugin(包含签名Launcher和安装包的两个执行逻辑)
  • 再声明nsis-maven-plugin

同时,将sign-launcher的执行阶段改为package,并确保它是exec-maven-plugin中第一个执行的任务,这样在NSIS生成安装包前,先完成Launcher的签名。

修改后的完整插件配置

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        ... <!-- 保留原有无关配置 -->
    </plugin>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>bundle-project-sources</id>
                <!-- 调整到prepare-package阶段,提前完成文件部署 -->
                <phase>prepare-package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>${basedir}/src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    <plugin> <!-- 先声明exec插件,确保签名Launcher在NSIS前执行 -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <!-- 第一个执行:签名Launcher,绑定到package阶段 -->
            <execution>
                <id>sign-launcher</id>
                <phase>package</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>${deployment.windowskit.path}\signtool.exe</executable>
                    <arguments>
                        <argument>sign</argument>
                        <argument>/f</argument>
                        <argument>${deployment.resources.signature.location}</argument>
                        <argument>/fd</argument>
                        <argument>certHash</argument>
                        <argument>/p</argument>
                        <argument>${deployment.resources.signature.secret}</argument>
                        <argument>${project.build.directory}\${project.build.finalName}\AppFolder\Launcher.exe</argument>
                    </arguments>
                </configuration>                        
            </execution>                    
            <!-- 第二个执行:签名安装包,绑定到package阶段,在NSIS之后执行 -->
            <execution>
                <id>sign-installer</id>
                <phase>package</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>${deployment.windowskit.path}\signtool.exe</executable>
                    <arguments>
                        <argument>sign</argument>
                        <argument>/f</argument>
                        <argument>${deployment.resources.signature.location}</argument>
                        <argument>/fd</argument>
                        <argument>certHash</argument>
                        <argument>/p</argument>
                        <argument>${deployment.resources.signature.secret}</argument>
                        <argument>${project.build.directory}\${project.build.finalName}.exe</argument>
                    </arguments>
                </configuration>                        
            </execution>
        </executions>
    </plugin>

    <plugin> <!-- 后声明NSIS插件,确保在Launcher签名后执行 -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>nsis-maven-plugin</artifactId>
        <version>1.0-alpha-1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>generate-headerfile</goal>
                    <goal>make</goal>
                </goals>
                <configuration>
                    <makensisBin>${package.nsisbinpath}</makensisBin> <!-- specify this in your settings.xml! -->
                    <scriptFile>target/${project.build.finalName}\Setup.nsi</scriptFile>
                    <outputFile>${project.build.finalName}.exe</outputFile>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    <plugin> <!-- Deploy to scp production server -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        ... <!-- 保留原有无关配置 -->
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        ... <!-- 保留原有无关配置 -->
    </plugin>
    <plugin> <!-- skip the regular deploy plugin - we don't want this build winding up in Nexus -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        ... <!-- 保留原有无关配置 -->
    </plugin>
</plugins>

执行顺序验证

修改后,Maven的执行流程会变为:

  1. prepare-package阶段:执行maven-assembly-plugin完成target目录部署
  2. package阶段:
    • 先执行exec-maven-plugin的sign-launcher任务,对Launcher.exe签名
    • 再执行nsis-maven-plugin生成NSIS安装包
    • 最后执行exec-maven-plugin的sign-installer任务,对安装包签名

内容的提问来源于stack exchange,提问作者Kevin Day

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 04:05:56