ANT와 함께 한 파일을 다른 파일에 포함시키는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/167990

  •  03-07-2019
  •  | 
  •  

문제

작은 웹 앱 프로젝트 (ColdFusion)를 개발하고 있으며 개발 중에 프로젝트를 여러 파일로 분할하려고하지만 완료시 하나의 파일 만 배포하려고합니다.

예를 들어 외부 파일에 대한 참조가 있습니다.

<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
<link type="text/css" rel="stylesheet" href="project.css" />

프로젝트를 구축 할 때 파일을 단일 완제품 파일에 포함시키고 내장하고 싶습니다.

<script type="text/javascript">eval(function(p,a,c,k,e,r) [...]</script>
<style type="text/css">div{font:normal;} [...]</style>

어쨌든, 개미가 이것을하는 기본 방법이있는 것처럼 보이지 않습니다. 누구든지 아는가?

도움이 되었습니까?

해결책 3

몇 시간 동안 해킹 한 후 내 자신의 질문에 대답 ...

<script language="groovy" src="build.groovy" />

이 그루비 스크립트는 참조 된 JavaScript 또는 CSS 파일을 파일 내용 자체로 대체합니다.

f = new File("${targetDir}/index.cfm")
fContent = f.text
fContent = jsReplace(fContent)
fContent = cssReplace(fContent)
f.write(fContent)

// JS Replacement
def jsReplace(htmlFileText) {
    println "Groovy: Replacing Javascript includes"
    // extract all matched javascript src links
    def jsRegex = /<script [^>]*src=\"([^\"]+)\"><\/script>/
    def matcher = (htmlFileText =~ jsRegex)
    for (i in matcher) {
        // read external files in
        def includeText = new File(matcher.group(1)).text
        // sanitize the string for being regex replace string (dollar signs like jQuery/Prototype will screw it up)
        includeText = java.util.regex.Matcher.quoteReplacement(includeText)
        // weak compression (might as well)
        includeText = includeText.replaceAll(/\/\/.*/, "") // remove single-line comments (like this!)
        includeText = includeText.replaceAll(/[\n\r\f\s]+/, " ") // replace all whitespace with single space
        // return content with embedded file
        htmlFileText = htmlFileText.replaceFirst('<script [^>]*src="'+ matcher.group(1) +'"[^>]*></script>', '<script type="text/javascript">'+ includeText+'</script>');
    }
    return htmlFileText;
}

// CSS Replacement
def cssReplace(htmlFileText) {
    println "Groovy: Replacing CSS includes"
    // extract all matched CSS style href links
    def cssRegex = /<link [^>]*href=\"([^\"]+)\"[^>]*>(<\/link>)?/
    def matcher = (htmlFileText =~ cssRegex)
    for (i in matcher) {
        // read external files in
        def includeText = new File(matcher.group(1)).text
        // compress CSS
        includeText = includeText.replaceAll(/[\n\r\t\f\s]+/, " ")
        // sanitize the string for being regex replace string (dollar signs like jQuery/Prototype will screw it up)
        includeText = java.util.regex.Matcher.quoteReplacement(includeText)
        // return content with embedded file
        htmlFileText = htmlFileText.replaceFirst('<link [^>]*href="'+ matcher.group(1) +'"[^>]*>(<\\/link>)?', '<style type=\"text/css\">'+ includeText+'</style>');
    }
    return htmlFileText;
}

그래서 나는 그것이 나를 위해 그렇게 생각합니다. 꽤 잘 작동하고 있으며 확장 가능합니다. 확실히 최고의 그루비는 아니지만, 그것은 나의 첫 번째 중 하나입니다. 또한 컴파일하려면 몇 개의 클래스 경로 된 항아리가 필요했습니다. 나는 그것을 잃어 버렸지 만 Javax.scripting 엔진, groovy-engine.jar 및 groovy-all-1.5.6.jar라고 생각합니다.

다른 팁

이것이 당신이 원하는 것을합니까?

<property
    name="filename"
    value="jquery-1.2.6.pack.js"
/>

<loadfile
    property="contents"
    srcfile="${filename}"
/>

<replace dir=".">
    <include name="index.cfm"/>
    <replacetoken><![CDATA[<script type="text/javascript" src="${filename}"></script>]]></replacetoken>
    <replacevalue><![CDATA[<script type="text/javascript">${contents}</script>]]></replacevalue>
</replace>

순수한 개미의 솔루션은 다음을 시도하십시오.

<target name="replace">
    <property name="js-filename" value="jquery-1.2.6.pack.js"/>
    <property name="css-filename" value="project.css"/>
    <loadfile property="js-file" srcfile="${js-filename}"/>
    <loadfile property="css-file" srcfile="${css-filename}"/>
    <replace file="input.txt">
        <replacefilter token="&lt;script type=&quot;text/javascript&quot; src=&quot;${js-filename}&quot;&gt;&lt;/script&gt;" value="&lt;script type=&quot;text/javascript&quot;&gt;${js-file}&lt;/script&gt;"/>
        <replacefilter token="&lt;link type=&quot;text/css&quot; rel=&quot;stylesheet&quot; href=&quot;${css-filename}&quot; /&gt;" value="&lt;style type=&quot;text/css&quot;&gt;${css-file}&lt;/style&gt;"/>
    </replace>
</target>

나는 그것을 테스트했고, 예상대로 작동했습니다. 대체 할 텍스트와 대신 삽입 값에서 모든 문자 '<', '>'및 ' "'는 <,> 및"로 인용해야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top