在maven构建阶段更新jsp脚本标记 [英] updating jsp script tag during maven build phase

查看:141
本文介绍了在maven构建阶段更新jsp脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新src / main / webapp / jsp下的jsp文件中所有脚本标签内的内容。
如何在maven构建阶段执行此操作?

I want to update content inside all script tags in jsp files present under src/main/webapp/jsp. How to do this during maven build phase?

我使用的是java + spring + maven堆栈。

I am using java+spring+maven stack.

好的,这是我想要实现的例子:

Ok here is example of what I want to achieve:

源代码:

<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="s" uri="/struts-tags"  %>
<script type="text/javascript" src="js/core/validator.js"></script>
<script type="text/javascript" src="js/app/util/core-util.js"></script>
<div id="dataContainer">
</div>

在maven构建之后,这应该出现在目标文件夹中

After maven build, this should be present in target folder

<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="s" uri="/struts-tags"  %>
<script type="text/javascript" src="js/core/validator.js?version='<MD5SUM-of-js/core/validator.js>'"></script>
<script type="text/javascript" src="js/app/util/core-util.js?version='<MD5SUM-of-js/app/util/core-util.js>'"></script>
<div id="dataContainer">
</div>

请注意src =末尾的版本参数。

Please note the version parameter at the end of src="".

更新:最后,我能够按照以下方式开展工作。如果有的话,请随意建议替代。

Update: Finally, I was able to make it work following way. Feel free to suggest alternative if any.


  1. 准备shell脚本以生成类似这样的属性文件

  1. Prepared shell script to generate properties file something like this

js / core / validator.js = js / core / validator.js?version \ = MD5SUM-of-js / core / validator.js

js / app / util / core-util.js = js / app / util / core-util.js?version \ = MD5SUM-of-js / app / util / core-util.js

js/core/validator.js=js/core/validator.js?version\=MD5SUM-of-js/core/validator.js
js/app/util/core-util.js=js/app/util/core-util.js?version\=MD5SUM-of-js/app/util/core-util.js

配置maven-replacer-plugin以将此属性文件用作标记值映射并过滤target / app / jsp文件夹下的所有jsp文件。

Configured maven-replacer-plugin to use this properties file as token value map and filter all the jsp files present under target/app/jsp folder.


推荐答案

你没有告诉你需要什么样的替代品,但是......

You didn't tell what kind of replacement you need, but...

对于很多情况,你可以使用常规的maven资源插件并打开过滤。这样它将用maven运行时属性替换任何 $ {values}

For lot of the cases, you can just use regular maven resources plugin and turn filtering on. That way it will replace any ${values} with maven runtime properties.

有关插件的文档

对于.jsps,您可能已经在使用war插件。使用war插件就像

For .jsps, you are possibly already using war plugin. With war plugin it is something like

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <webResources>
                <resource>
                    <filtering>true</filtering>
                    <targetPath>WEB-INF</targetPath>
                    <directory>src/main/resources/WEB-INF</directory>
                    <includes>
                        <include>*.jsp</include>
                    </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>

然后你的JSP中有这样的东西:

Then you'd have something like this in your JSPs:

<script src="${mypath}/test.js"></script>

在maven pom中:

And in maven pom:

<properties>
   <mypath>testpath</mypath>
</properties>

取决于您的项目配置。 (例如,经常使用src / main / webapp)

Depending on your project configuration. (src/main/webapp is often used as well, for example)

编辑:您在评论中添加了你有一个计算md5的shell脚本,你想使用它。我不知道这样做的优雅方式,所以建议一种不那么优雅的方式:使用groovy插件来执行脚本并获取属性的值。

you have added in the comments that you have a shell script counting md5 and you want to use that. I don't know an elegant way of doing that, so suggesting a less elegant way: use groovy plugin to execute the script and get the value to a property.

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <providerSelection>2.0</providerSelection>
                <properties>
                    <script>path/to/your/shell-script.sh</script>
                </properties>
                <!-- if the script prints its result to stdout -->
                <source>
                    def command = project.properties.script
                    def process = command.execute()
                    process.waitFor()

                    def result = process.in.text.trim()
                    project.properties.md5Value = result
                </source>
                <!-- if the script prints the result to a file -->
                <!-- note that you have to define this result file name somewhere -->
                <source>
                    def command = project.properties.script
                    def process = command.execute()
                    process.waitFor()

                    def resultfile = new File(project.properties.result_file)
                    project.properties.md5Value = resultfile.getText()
                </source>
                <!-- only use one or the other script block! -->
            </configuration>
        </execution>
    </executions>
</plugin>

这篇关于在maven构建阶段更新jsp脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆