如何在Maven中运行PowerShell脚本

4

我目前正在处理一个使用maven的问题。我尝试在构建软件时使用maven更新文件。由于某些原因,我必须使用powershell脚本,并在使用mvn构建时运行它。以下是我的代码:

<exec executable="powershell.exe"
                      spawn="true">
                    <arg value="/c"/>
                    <arg value="myReplace.ps1"/>
                    <arg value="../../the/path/to/the/directory/" />
                    <arg value ="filename"/>
                    <arg value="value1" />
                    <arg value="value2" />
                    <arg value="value3" />
             </exec>

它没有按照预期工作,有人能帮我吗?


2
请提供错误信息。 - Jérome Pieret
这看起来很像Ant。你说过(并标记了)Maven。怎么回事?难道你不想使用Exec Maven插件吗? - Sander Verhagen
2个回答

4

这条消息有点过时,但我在最近几天的练习中做了这个。关键是使用exec插件,如所述。下面是一个项目pom.xml的示例:

下面的代码启动了PowerShell模块脚本的代码签名。请注意,调用PowerShell函数的命令字符串必须以“&”开头,而Maven无法正确处理此字符串。需要对其进行转义才能解决问题。对于更复杂的操作,请调用PS脚本。

...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3.2</version>
            <executions>
                <execution>
                    <id>scripts-package</id>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <phase>prepare-package</phase>
                    <configuration>
                <!-- optional -->
                    <workingDirectory>/Temp</workingDirectory>
                    <arguments>
                        <argument>-command</argument>
                        <argument>"&amp; { Set-AuthenticodeSignature '${project.build.directory}/Logging_Functions/*.psm1' @(Get-ChildItem ${project.build.certificate.path} -codesigning)[0]"}</argument>
                    </arguments>
                    </configuration>
                </execution>

0

这样可以避免使用 PowerShell 时出现 bug 导致 StdIn 卡住的问题。

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
                <useMavenLogger>true</useMavenLogger>
            </configuration>
            <executions>
                <execution>
                    <id>run-my-script</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>powershell.exe</executable>
                        <arguments>
                            <argument>-InputFormat</argument>
                            <argument>None</argument>
                            <argument>-File</argument>
                            <argument>${basedir}/my_script.ps1</argument>
                        </arguments>
                    </configuration>
                </execution>
            <executions>
        <plugin>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接