简体   繁体   English

批处理文件以编辑文本文件

[英]batch file to edit a text file

I need this to deploy a web application using a war file. 我需要使用war文件来部署Web应用程序。

I have worked out how to create a war file using jar command. 我已经解决了如何使用jar命令创建war文件。 But when a user deploys the web application a certain string in the web.xml file must be updated to reflect the users environment. 但是,当用户部署Web应用程序时,必须更新web.xml文件中的某些字符串以反映用户环境。

Eg in web.xml file I have entry (example): 例如在web.xml文件中,我有条目(示例):

<init-param>
 <param-name>colour</param-name>
 <param-value>red</param-value>
</init-param>

I want to ask user for colour and then update war file. 我想询问用户颜色,然后更新war文件。 Obviously so user deploys the correct thing. 显然,这样用户可以部署正确的东西。 I can update the file using jar uf - no problem there. 我可以使用jar uf更新文件-那里没有问题。 Eg the text above would be inserted in a set location in the text file. 例如,上面的文本将插入到文本文件中的设置位置。 Being specific, would be after the text . 具体而言,将在文本之后。

I am happy to do this on command line. 我很高兴在命令行上执行此操作。 Eg something like this: 例如这样的事情:

  1. Prompt user for string. 提示用户输入字符串。
  2. update string in web.xml 更新web.xml中的字符串
  3. update web.xml file in war deployment file. 更新war部署文件中的web.xml文件。
  4. Then happy for user to manually copy war file to correct location. 然后很高兴让用户手动将war文件复制到正确的位置。

How would I program this? 我将如何编程? For now, Windows only (but will want linux support shortly). 目前,仅Windows(但不久将需要Linux支持)。 A batch file? 批处理文件? Any suggestions for how to approach? 有关如何处理的任何建议?

按照本示例中的说明使用Maven资源过滤: http : //maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

search and replace with GNU sed : 搜索并替换为GNU sed

(for your Q1 & Q2) (针对您的第一季度和第二季度)

@echo off&setlocal
REM set the old color in a variable
set "oldstring=red"
REM prompt the user for a string
set/p "string=enter string: "
REM update string in web.xml
sed -i "s/%oldstring%/%string%/i" web.xml
type web.xml

.. output is: ..输出为:

enter string: green
<init-param>
 <param-name>colour</param-name>
 <param-value>green</param-value>
</init-param>

I have no clue of war files . 我没有war files线索。 Please explain, then I will improve my code. 请解释,然后我将改进我的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM