自动化 Eclipse“Yui 压缩机...”

发布于 2025-01-05 09:28:36 字数 1231 浏览 6 评论 0原文

Eclipse PDT 在文件的上下文菜单中有这个方便的内置 Yui 压缩机。但是,当构建使用多个此类文件的 Web 应用程序时,每次更新后手动压缩文件会变得很乏味。它甚至不记得哪些文件压缩为哪些文件名,因此您必须再次输入。

是否可以在 Eclipse 中轻松地自动执行此过程,以便您可以单击“构建”或其他内容,所有指定的文件将被压缩到其目标?

Eclipse 中有关 Yui 功能的文档确实存在真空。

我正在使用 Eclipse Indigo 3.7.0 和 PDT 3.0.0.v20110516-11,但自 Galileo 以来,PDT 和 Yui 选项都非常相似

-update-

自动化解决方案,不是 Eclipse 原生的,但接近:

  • GNU make makefile

    %-min.js: %.js
        ${java} -jar ${compressor} $< -o ${<:.js=-min.js}
    
  • Windows 批处理

    FOR %f IN (*.js) DO java -jar yuicompressor.jar %f -o 部署\%f
    
  • Linux Shellscript

    <前><代码>ls -1 *.js | awk '{printf("java -jar yuicompressor.jar %s -o 部署/%s",$1,$1)}' | /bin/sh
  • ANT 文件

    看这里: http://www.ubik-ingenierie.com/ubikwiki/ index.php?title=Minifying_JS/CSS
    如何在此处粘贴 html/xml?

另请参阅这个问题。

Eclipse PDT has this handy built-in Yui Compressor in the context menu for files. But when building a webapp that uses multiple such files, it becomes tedious to compress the files manually after each update. It doesn't even remember what files compress to which filenames, so you have to enter that again.

Is it possible to automate this process easily within Eclipse, so you can click on "build" or something and all specified files will be compressed to their targets?

There is really a vacuum concerning documentation about this Yui feature in Eclipse.

I am using Eclipse Indigo 3.7.0 with PDT 3.0.0.v20110516-11, but both PDT and the Yui option have remained pretty similar since Galileo

-update-

Automation solutions, not Eclipse-native but close:

  • GNU make makefile

    %-min.js: %.js
        ${java} -jar ${compressor} 
    lt; -o ${<:.js=-min.js}
    
  • Windows Batch

    FOR %f IN (*.js) DO java -jar yuicompressor.jar %f -o deploy\%f
    
  • Linux Shellscript

    ls -1 *.js | awk '{printf("java -jar yuicompressor.jar %s -o deploy/%s",$1,$1)}' | /bin/sh
    
  • ANT file

    See here:
    http://www.ubik-ingenierie.com/ubikwiki/index.php?title=Minifying_JS/CSS
    How do I paste html/xml here?

Also see this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半城柳色半声笛 2025-01-12 09:28:36

好吧,我实际上下载了 jar 并创建了一个 ANT 任务来压缩 CSS 和 JS。我在 Tomcat 上工作,因此它包括上传内容、清理工作/目录等任务。希望有帮助。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<project name="MyProject" default="subir">

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <property name="project.name" value="MyProject" />

    <property name="src.dir" location="src" />
    <property name="web.dir" location="WebContent" />
    <property name="lib.dir" location="WebContent/WEB-INF/lib" />
    <property name="bin.dir" location="WebContent/WEB-INF/classes" />

    <property name="minify.dir" location="minified" />
    <property name="minify.sourcedir" location="${skin.dir}/resources/" />
    <!-- <property name="minify.sourcedir" location="${web.dir}/resources/" /> -->

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <property prefix="env" file="build.properties" />

    <path id="project.classpath">
        <pathelement location="${src.dir}" />
        <fileset dir="${lib.dir}">
            <include name="*.jar" />
        </fileset>
    </path>

    <path id="yui.classpath.minifier">
        <fileset dir="${lib.dir}">
            <include name="YUIAnt.jar" />
            <include name="yuicompressor-2.4.2.jar" />
        </fileset>
    </path>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="checkTomcatHome">
        <condition property="local.tomcathome.exists">
            <available file="${env.TOMCAT_PRUEBAS}" type="dir" />
    </condition>
    </target>

    <target name="subir" depends="checkTomcatHome" if="local.tomcathome.exists">

        <echo message="El servidor Tomcat destino existe, buscando archivos para copia en el proyecto y en en OsmoCore/WebContent..." />

        <copy todir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}">
            <fileset dir="${web.dir}">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <!--<echo message="Buscando archivos para copia en ${env.TOMCAT_PRUEBAS}/webapps/${project.name}/WEB-INF/classes..." />
        <copy todir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}/WEB-INF/classes">
            <fileset dir="${bin.dir}">
                <include name="**/*.*" />
            </fileset>
        </copy>-->

    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="WAR">

        <delete file="${project.name}.war"/>
        <delete dir="TMP_WAR"/>
        <mkdir dir="TMP_WAR"/>


        <copy todir="TMP_WAR">
            <fileset dir="${web.dir}">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <copy todir="TMP_WAR/WEB-INF/classes">
            <fileset dir="${bin.dir}">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <delete dir="${project.name}.war"/>

        <zip destfile="${project.name}.war">
            <zipfileset dir="TMP_WAR">
                <include name="**/*.*" />
            </zipfileset>
        </zip>

        <delete dir="TMP_WAR"/>

    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="subirCompleto" depends="checkTomcatHome" if="local.tomcathome.exists">
        <echo message="El servidor Tomcat destino existe, buscando carpetas Work y ${project.name} en Webapps, para eliminar" />
        <delete dir="${env.TOMCAT_PRUEBAS}/work" />
        <delete dir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}" />
        <antcall target="subir" />
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="borrarWork" depends="checkTomcatHome" if="local.tomcathome.exists">
        <echo message="El servidor Tomcat destino existe, buscando carpeta Work a eliminar..." />
        <delete dir="${env.TOMCAT_PRUEBAS}/work"/>
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="css.minify">

        <echo message="Creando directorio temporal ${minify.dir} (si no existe aún)..." />
        <mkdir dir="${minify.dir}" />

        <echo message="Borrando versión anterior de la carpeta temporal ${minify.dir}/css-min/..." />
        <delete dir="${minify.dir}/css-min/" />
        <echo message="Creando carpeta temporal ${minify.dir}/css-min/..." />
        <mkdir dir="${minify.dir}/css-min/" />

        <echo message="Copiando estructura de ${web.dir}/resources/styles/ en carpeta temporal..." />
        <copy todir="${minify.dir}/css-min/">
            <fileset dir="${minify.sourcedir}/styles/">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <echo message="Borrando los CSS copiados a la carpeta temporal..." />
        <delete>
            <fileset dir="${minify.dir}/css-min/" >
                <include name="**/*.css"/>
            </fileset>
        </delete>

        <echo message="Comprimiendo!!!..." />
        <apply executable="java" parallel="false" dest="${minify.dir}/css-min/">
            <fileset dir="${minify.sourcedir}/styles/">
                <include name="**/*.css"/>
            </fileset>
            <arg line="-jar"/>
            <arg path="${lib.dir}/yui_compressor/yuicompressor-2.4.7.jar"/>
            <arg line="--line-break 0"/>
            <arg line="--type css"/>
            <arg line="--charset ISO-8859-1"/>
            <arg line="--nomunge"/>
            <!-- <arg line="- -verbose"/> -->
            <srcfile />
            <arg line="-o"/>
            <mapper type="glob" from="*.css" to="*.css"/>
            <targetfile />
        </apply>
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="js.minify">

        <echo message="Creando directorio temporal ${minify.dir} (si no existe aún)..." />
        <mkdir dir="${minify.dir}" />

        <echo message="Borrando versión anterior de la carpeta temporal ${web.dir}/resources/js-min/..." />
        <delete dir="${minify.dir}/js-min/" />
        <echo message="Creando carpeta temporal ${web.dir}/resources/js-min/..." />
        <mkdir dir="${minify.dir}/js-min/" />

        <echo message="Copiando estructura de ${web.dir}/resources/scripts/ en carpeta temporal..." />
        <copy todir="${minify.dir}/js-min/">
            <fileset dir="${minify.sourcedir}/scripts/">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <echo message="Borrando los JS copiados a la carpeta temporal..." />
        <delete>
            <fileset dir="${minify.dir}/js-min/" >
                <include name="**/*.js"/>
            </fileset>
        </delete>

        <echo message="Comprimiendo!!!..." />
        <apply executable="java" parallel="false" dest="${minify.dir}/js-min/">

            <fileset dir="${minify.sourcedir}/scripts">
                <include name="**/*.js"/>
            </fileset>
            <arg line="-jar"/>
            <arg path="${lib.dir}/yui_compressor/yuicompressor-2.4.7.jar"/>
            <arg line="--line-break 0"/>
            <arg line="--type js"/>
            <arg line="--charset ISO-8859-1"/>
            <arg line="--nomunge"/>
            <!--<arg line="- -verbose"/>-->
            <srcfile />
            <arg line="-o"/>
            <mapper type="glob" from="*.js" to="*.js"/>
            <targetfile />
        </apply>

    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="js.minified.subir" depends="checkTomcatHome" if="local.tomcathome.exists">
        <echo message="El servidor Tomcat destino existe, copiando archivos 'encogidos' a  ${env.TOMCAT_PRUEBAS}/webapps/${project.name}/resources/scripts..." />
        <copy todir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}/resources/scripts" overwrite="yes">
            <fileset dir="${minify.dir}/js-min/">
                <include name="**/*.*" />
            </fileset>
        </copy>
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="css.minified.subir" depends="checkTomcatHome" if="local.tomcathome.exists">
        <echo message="El servidor Tomcat destino existe, copiando archivos 'encogidos' a  ${env.TOMCAT_PRUEBAS}/webapps/${project.name}/resources/styles..." />
        <copy todir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}/resources/styles" overwrite="yes">
            <fileset dir="${minify.dir}/css-min/">
                <include name="**/*.*" />
            </fileset>
        </copy>
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="css.minified.process">
        <echo message="Reduciendo el tamaño de los archivos CSS..." />
        <antcall target="css.minify" />

        <echo message="Cargando los archivos CSS..." />
        <antcall target="css.minified.subir" />

        <echo message="Borrando la carpeta temporal..." />
        <delete dir="${minify.dir}" />
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="js.minified.process">
        <echo message="Reduciendo el tamaño de los archivos JS..." />
        <antcall target="js.minify" />

        <echo message="Cargando los archivos JS..." />
        <antcall target="js.minified.subir" />

        <echo message="Borrando la carpeta temporal..." />
        <delete dir="${minify.dir}" />
    </target>
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

</project>

Well, I actually downloaded the jar and created an ANT task to to CSS and JS compression. I work on Tomcat, so it includes tasks to upload stuff, clean work/ directory and things like that. Hope it helps.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project>
<project name="MyProject" default="subir">

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <property name="project.name" value="MyProject" />

    <property name="src.dir" location="src" />
    <property name="web.dir" location="WebContent" />
    <property name="lib.dir" location="WebContent/WEB-INF/lib" />
    <property name="bin.dir" location="WebContent/WEB-INF/classes" />

    <property name="minify.dir" location="minified" />
    <property name="minify.sourcedir" location="${skin.dir}/resources/" />
    <!-- <property name="minify.sourcedir" location="${web.dir}/resources/" /> -->

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <property prefix="env" file="build.properties" />

    <path id="project.classpath">
        <pathelement location="${src.dir}" />
        <fileset dir="${lib.dir}">
            <include name="*.jar" />
        </fileset>
    </path>

    <path id="yui.classpath.minifier">
        <fileset dir="${lib.dir}">
            <include name="YUIAnt.jar" />
            <include name="yuicompressor-2.4.2.jar" />
        </fileset>
    </path>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="checkTomcatHome">
        <condition property="local.tomcathome.exists">
            <available file="${env.TOMCAT_PRUEBAS}" type="dir" />
    </condition>
    </target>

    <target name="subir" depends="checkTomcatHome" if="local.tomcathome.exists">

        <echo message="El servidor Tomcat destino existe, buscando archivos para copia en el proyecto y en en OsmoCore/WebContent..." />

        <copy todir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}">
            <fileset dir="${web.dir}">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <!--<echo message="Buscando archivos para copia en ${env.TOMCAT_PRUEBAS}/webapps/${project.name}/WEB-INF/classes..." />
        <copy todir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}/WEB-INF/classes">
            <fileset dir="${bin.dir}">
                <include name="**/*.*" />
            </fileset>
        </copy>-->

    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="WAR">

        <delete file="${project.name}.war"/>
        <delete dir="TMP_WAR"/>
        <mkdir dir="TMP_WAR"/>


        <copy todir="TMP_WAR">
            <fileset dir="${web.dir}">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <copy todir="TMP_WAR/WEB-INF/classes">
            <fileset dir="${bin.dir}">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <delete dir="${project.name}.war"/>

        <zip destfile="${project.name}.war">
            <zipfileset dir="TMP_WAR">
                <include name="**/*.*" />
            </zipfileset>
        </zip>

        <delete dir="TMP_WAR"/>

    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="subirCompleto" depends="checkTomcatHome" if="local.tomcathome.exists">
        <echo message="El servidor Tomcat destino existe, buscando carpetas Work y ${project.name} en Webapps, para eliminar" />
        <delete dir="${env.TOMCAT_PRUEBAS}/work" />
        <delete dir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}" />
        <antcall target="subir" />
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="borrarWork" depends="checkTomcatHome" if="local.tomcathome.exists">
        <echo message="El servidor Tomcat destino existe, buscando carpeta Work a eliminar..." />
        <delete dir="${env.TOMCAT_PRUEBAS}/work"/>
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="css.minify">

        <echo message="Creando directorio temporal ${minify.dir} (si no existe aún)..." />
        <mkdir dir="${minify.dir}" />

        <echo message="Borrando versión anterior de la carpeta temporal ${minify.dir}/css-min/..." />
        <delete dir="${minify.dir}/css-min/" />
        <echo message="Creando carpeta temporal ${minify.dir}/css-min/..." />
        <mkdir dir="${minify.dir}/css-min/" />

        <echo message="Copiando estructura de ${web.dir}/resources/styles/ en carpeta temporal..." />
        <copy todir="${minify.dir}/css-min/">
            <fileset dir="${minify.sourcedir}/styles/">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <echo message="Borrando los CSS copiados a la carpeta temporal..." />
        <delete>
            <fileset dir="${minify.dir}/css-min/" >
                <include name="**/*.css"/>
            </fileset>
        </delete>

        <echo message="Comprimiendo!!!..." />
        <apply executable="java" parallel="false" dest="${minify.dir}/css-min/">
            <fileset dir="${minify.sourcedir}/styles/">
                <include name="**/*.css"/>
            </fileset>
            <arg line="-jar"/>
            <arg path="${lib.dir}/yui_compressor/yuicompressor-2.4.7.jar"/>
            <arg line="--line-break 0"/>
            <arg line="--type css"/>
            <arg line="--charset ISO-8859-1"/>
            <arg line="--nomunge"/>
            <!-- <arg line="- -verbose"/> -->
            <srcfile />
            <arg line="-o"/>
            <mapper type="glob" from="*.css" to="*.css"/>
            <targetfile />
        </apply>
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="js.minify">

        <echo message="Creando directorio temporal ${minify.dir} (si no existe aún)..." />
        <mkdir dir="${minify.dir}" />

        <echo message="Borrando versión anterior de la carpeta temporal ${web.dir}/resources/js-min/..." />
        <delete dir="${minify.dir}/js-min/" />
        <echo message="Creando carpeta temporal ${web.dir}/resources/js-min/..." />
        <mkdir dir="${minify.dir}/js-min/" />

        <echo message="Copiando estructura de ${web.dir}/resources/scripts/ en carpeta temporal..." />
        <copy todir="${minify.dir}/js-min/">
            <fileset dir="${minify.sourcedir}/scripts/">
                <include name="**/*.*" />
            </fileset>
        </copy>

        <echo message="Borrando los JS copiados a la carpeta temporal..." />
        <delete>
            <fileset dir="${minify.dir}/js-min/" >
                <include name="**/*.js"/>
            </fileset>
        </delete>

        <echo message="Comprimiendo!!!..." />
        <apply executable="java" parallel="false" dest="${minify.dir}/js-min/">

            <fileset dir="${minify.sourcedir}/scripts">
                <include name="**/*.js"/>
            </fileset>
            <arg line="-jar"/>
            <arg path="${lib.dir}/yui_compressor/yuicompressor-2.4.7.jar"/>
            <arg line="--line-break 0"/>
            <arg line="--type js"/>
            <arg line="--charset ISO-8859-1"/>
            <arg line="--nomunge"/>
            <!--<arg line="- -verbose"/>-->
            <srcfile />
            <arg line="-o"/>
            <mapper type="glob" from="*.js" to="*.js"/>
            <targetfile />
        </apply>

    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="js.minified.subir" depends="checkTomcatHome" if="local.tomcathome.exists">
        <echo message="El servidor Tomcat destino existe, copiando archivos 'encogidos' a  ${env.TOMCAT_PRUEBAS}/webapps/${project.name}/resources/scripts..." />
        <copy todir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}/resources/scripts" overwrite="yes">
            <fileset dir="${minify.dir}/js-min/">
                <include name="**/*.*" />
            </fileset>
        </copy>
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="css.minified.subir" depends="checkTomcatHome" if="local.tomcathome.exists">
        <echo message="El servidor Tomcat destino existe, copiando archivos 'encogidos' a  ${env.TOMCAT_PRUEBAS}/webapps/${project.name}/resources/styles..." />
        <copy todir="${env.TOMCAT_PRUEBAS}/webapps/${project.name}/resources/styles" overwrite="yes">
            <fileset dir="${minify.dir}/css-min/">
                <include name="**/*.*" />
            </fileset>
        </copy>
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="css.minified.process">
        <echo message="Reduciendo el tamaño de los archivos CSS..." />
        <antcall target="css.minify" />

        <echo message="Cargando los archivos CSS..." />
        <antcall target="css.minified.subir" />

        <echo message="Borrando la carpeta temporal..." />
        <delete dir="${minify.dir}" />
    </target>

    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

    <target name="js.minified.process">
        <echo message="Reduciendo el tamaño de los archivos JS..." />
        <antcall target="js.minify" />

        <echo message="Cargando los archivos JS..." />
        <antcall target="js.minified.subir" />

        <echo message="Borrando la carpeta temporal..." />
        <delete dir="${minify.dir}" />
    </target>
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->

</project>
誰認得朕 2025-01-12 09:28:36

您还可以使用 Node.js 进行批量缩小。但是,此选项仅适用于 Linux 用户(目前)。

每当某些软件包严重缺乏便利性(Eclipse)或构建器批处理格式太疯狂(ANT)时,就会有人聪明地出现并创造出一些很棒的东西。

在这种情况下,

非常棒。

只是想我会更新这个问题并添加答案。

You can also batch-minify using Node.js. However, this option is only for Linux users (for now).

Every time some software package is horribly lacking a convenience (Eclipse) or a builder batch format is just crazy (ANT), someone clever comes along and creates something awesome.

In this case,

which is awesome.

Just thought I'd update this question and add the answer.

一花一树开 2025-01-12 09:28:36

您可以通过批量配置和添加外部工具来选择和“构建”js/css 资源(适用于 Windows 用户)。

这样一来,只需按一个按钮即可缩小。 (它不是运行配置的一部分。)

先决条件:
- 必须安装Java。
- 您必须下载 yuicompressor。 (目前官方下载页面在这里:https://github.com/yui/yuicompressor/releases )

第 1 步:将“yuicompressor”和“minify.bat”(如下所示)添加到您的项目中。

输入图片此处描述

步骤2:配置名为“Minify”的外部工具(转到菜单“运行”->“外部工具”->“外部工具配置”)。

输入图片此处描述
输入图片此处描述
输入图片description here

步骤3:选择资源(js/css)或包含它们的目录,然后单击“缩小”。 js/css 文件将部署到同级“min”目录。

输入图片此处描述

(minify.bat)

@echo off

set target=%1
set yui=%2\WebContent\build\yuicompressor\yuicompressor-2.4.8.jar

:: %1 selected resource
:: %2 current project

:: dir
if exist %target%\ (
  cd /d %target%
  mkdir min 2>nul

  for %%f in (*.js *.css) do (
    echo Minifying "%%~ff"
    java -jar %yui% %%f -o min\%%f
  ) 

  goto end
) 

:: .js/.css
set pathNoExt=0
for /f %%i in ('dir /b %target%') do set pathNoExt=%%~ni

if not %pathNoExt%==0 (
  cd /d %~dp1
  mkdir min 2>nul

  for /f %%f in ('dir /b %pathNoExt%.js %pathNoExt%.css') do (
    echo Minifying "%%~ff"
    java -jar %yui% %%f -o min\%%f
  )
)

:end
echo Minified

You can select and "build" js/css resources by configuring and adding an External Tool with a batch (for Windows users).

This makes it possible to minify by just pressing one button. (It is not part of a run configuration.)

Preconditions:
- Java has to be installed.
- You must download yuicompressor. (Currently the official download page is here: https://github.com/yui/yuicompressor/releases )

Step1: Add "yuicompressor" and "minify.bat" (given below) to your project.

enter image description here

Step2: Configure an External Tool named "Minify" (Go to menu Run -> External Tools -> External Tools Configurations).

enter image description here
enter image description here
enter image description here

Step3: Select the resources (js/css) or the directory that contains them, and click "Minify". The js/css files will be deployed to the sibling "min" directory.

enter image description here

(minify.bat)

@echo off

set target=%1
set yui=%2\WebContent\build\yuicompressor\yuicompressor-2.4.8.jar

:: %1 selected resource
:: %2 current project

:: dir
if exist %target%\ (
  cd /d %target%
  mkdir min 2>nul

  for %%f in (*.js *.css) do (
    echo Minifying "%%~ff"
    java -jar %yui% %%f -o min\%%f
  ) 

  goto end
) 

:: .js/.css
set pathNoExt=0
for /f %%i in ('dir /b %target%') do set pathNoExt=%%~ni

if not %pathNoExt%==0 (
  cd /d %~dp1
  mkdir min 2>nul

  for /f %%f in ('dir /b %pathNoExt%.js %pathNoExt%.css') do (
    echo Minifying "%%~ff"
    java -jar %yui% %%f -o min\%%f
  )
)

:end
echo Minified
白况 2025-01-12 09:28:36

我想改进megre的答案(https://stackoverflow.com/a/53678752/760777)。他的解决方案有效,但可以改进。不过,这一切都归功于他。

步骤2:将批处理文件的位置更改为${project_loc}\build\yuicompressor.bat。
将批处理文件“yuicompressor.bat”放入项目文件夹的子文件夹“build”中。
或者将批处理文件放在 yuicompressor 的 jar 文件所在的同一文件夹中。在我的例子中,这将是 C:\Program Files (x86)\yuicompressor\yuicompressor.bat

要引用 css 和 js 文件的正确位置,您可以使用不同的变量, $resource_loc、$project_loc 或 $workspace_loc 与 $project_name 。不同版本的 Eclipse 似乎使用不同的变量。

注意:不要忘记使用双引号,以防路径中有空格。

我最终使用了工作区位置和项目名称。
我的Javascript文件位于名为assets/js的文件夹中,我的样式表位于名为assets/css的文件夹中

请参见屏幕截图:

在此处输入图像描述

我完全更改了批处理文件:

@ECHO OFF

REM Note: There is a bug in yuicompressor 2.4.8 causing yuicompressor not te be accessible 
REM       see https://stackoverflow.com/a/19339287/760777

SET yui="C:\Program Files (x86)\yuicompressor\yuicompressor-2.4.7.jar"
SET cssFolder=%1\css
SET jsFolder=%1\js

IF EXIST %cssFolder% (
  ECHO Processing folder "%cssFolder%" ... 
  CD %cssFolder%
  MKDIR min 2>NUL
  FOR %%f IN (*.css) DO (
    ECHO Minifying "%%~ff"
    java -jar %yui% %%f -o min\%%f 
  )
) ELSE (
  ECHO %cssFolder% does not exist. Put your css files in a subfolder "css" in the current project folder 
)

IF EXIST %jsFolder% (
  ECHO Processing folder "%jsFolder%" ... 
  CD %jsFolder%
  MKDIR min 2>NUL
  FOR %%f IN (*.js) DO (
    ECHO Minifying "%%~ff"
    java -jar %yui% %%f -o min\%%f
  ) 
) ELSE (
  ECHO %jsFolder% does not exist. Put your js files in a sub folder "js" in the current project folder 
)
ECHO Finished!

可能不必说,但要使用缩小的文件,你必须相应地修改 HTML / PHP 文件中的引用。

I would like to improve the answer of megre (https://stackoverflow.com/a/53678752/760777). His solution works, but can be improved. All credits to him though.

In step 2: Change the location of the batch file into ${project_loc}\build\yuicompressor.bat.
Place your batch file "yuicompressor.bat" in a subfolder "build" in your project folder.
Or place the batch file in the same folder the jar file of yuicompressor resides. In my case that would be C:\Program Files (x86)\yuicompressor\yuicompressor.bat

To refer to the right location of your css and js files, you can use different variables, $resource_loc, $project_loc or $workspace_loc with $project_name. Different versions of Eclipse seem to use different variables.

Note: Don't forget to use double quotes, in the case there are spaces in the path.

I ended up using the workspace location combinded with the project name.
My Javascript files are in a folder named assets/js and my stylesheets are in a folder named assets/css

See screenshot:

enter image description here

I changed the batch file completely:

@ECHO OFF

REM Note: There is a bug in yuicompressor 2.4.8 causing yuicompressor not te be accessible 
REM       see https://stackoverflow.com/a/19339287/760777

SET yui="C:\Program Files (x86)\yuicompressor\yuicompressor-2.4.7.jar"
SET cssFolder=%1\css
SET jsFolder=%1\js

IF EXIST %cssFolder% (
  ECHO Processing folder "%cssFolder%" ... 
  CD %cssFolder%
  MKDIR min 2>NUL
  FOR %%f IN (*.css) DO (
    ECHO Minifying "%%~ff"
    java -jar %yui% %%f -o min\%%f 
  )
) ELSE (
  ECHO %cssFolder% does not exist. Put your css files in a subfolder "css" in the current project folder 
)

IF EXIST %jsFolder% (
  ECHO Processing folder "%jsFolder%" ... 
  CD %jsFolder%
  MKDIR min 2>NUL
  FOR %%f IN (*.js) DO (
    ECHO Minifying "%%~ff"
    java -jar %yui% %%f -o min\%%f
  ) 
) ELSE (
  ECHO %jsFolder% does not exist. Put your js files in a sub folder "js" in the current project folder 
)
ECHO Finished!

Probably unnecessary to say, but to use the minified files, you have to modify the references in your HTML / PHP files accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文