基于maven的seajs打包

场景:

       业务相关的js使用seajs基础库,项目打包部署时需要使用seajs的spm命令将业务js逐个进行打包。

       为了避免新打包js文件的浏览器缓存,每次打包需要设置一个版本号文件夹,打包时将js文件打到版本号文件夹下。

       项目使用maven构建。

       因此,在使用maven命令打包执行,需要在配置文件中设置版本号和批量执行spm命令。

       附:需要替换版本号seajs打包的bat脚本文件(package-corejs.bat) 和 portable-config-maven-plugin配置(profiles/product.xml)如下。其中portable-config-maven-plugin插件能够在mvn package执行完之后替换war包中指定properties文件的内容。

       package-corejs.bat

@echo off
cd %~dp0
cd ..
set tmodDir="src\main\webapp\resources\js"
%需要设置的版本号%
set version=0.0.17
cd %tmodDir%

%改变系统编码方式,否则spm命令执行失败%
chcp 850

%清理需要生成的打包目录%
rd /q /s %version%

%create core folder%
md %version%\core
%copy core to target folder%
xcopy core %version%\core /e /y
%create template folder%
md %version%\template
%copy template to target folder%
xcopy template %version%\template /e /y

%跳转到core目录下%
cd core
%针对js文件,逐个执行打包命令%
for /D %%s in (*) do (
  echo package : %%s
  spm build -I %%s -O ../../%version% --idleading core/%%s
)

pause

       profiles/product.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<portable-config>
	<config-file path="WEB-INF/classes/application.properties">
                <!--需要配置的版本号-->
		<replace key='web.param.resource_version'>0.0.17</replace>
	</config-file>
</portable-config>

解决:

1. 使用maven的antrun插件,在maven生命周期中的process-resources阶段,读取控制台输入的版本号,并替换配置文件中的版本号信息。

2. 使用maven的antrun插件,在maven生命周期中的process-resources阶段,执行package-corejs.bat脚本,完成seajs文件的生成。

maven-antrun-plugin插件配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>prepare-package-antrun</id>
            <phase>process-resources</phase>
            <configuration>
                <!--在控制台提示输入静态文件当前的版本号信息,并根据输入的版本号替换相应的静态文件设置-->
                <target name="get new version">
                    <input addproperty="resource.version" defaultvalue="0.0.1">please input the resource new version : </input>
                    <!-- 创建临时文件存储输入的版本号信息 -->
                    <propertyfile file="${project.build.directory}\resource_version.properties">
                        <entry key="resource.version" value="${resource.version}"/>
                    </propertyfile>
                    <!-- 替换掉整个项目中打包命令的版本号 -->
                    <replaceregexp match="set version=(\d{1,}.){0,}\d{1,}" replace="set version=${resource.version}" flags="g" encoding="utf-8">
                        <fileset dir="${basedir}" includes="**/bin/package-corejs.bat"/>
                    </replaceregexp>
                    <!-- 替换掉整个项目中profile中xml配置的版本号 -->
                    <replaceregexp match=".web.param.resource_version..(\d{1,}.){0,}\d{1,}" replace="'web.param.resource_version'>${resource.version}" flags="g" encoding="utf-8">
                        <fileset dir="${basedir}" includes="**/profiles/*.xml"/>
                    </replaceregexp>
                    <!--执行core目录下js文件的打包命令-->
                    <echo>start execute package-corejs.bat</echo>
                    <exec executable="${basedir}/bin/package-corejs.bat"></exec>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

注:使用spm打包会将打包js文件中相对依赖的js文件都打进来,并且不会根据逻辑解析require的依赖,所以可能会导致在初始化父模块的时候,将所有子模块的依赖都加载进来。而如果使用require.async会实时加载静态文件。最终是否使用spm打包,需要结合实际情况配置。不使用package.json,spm就不会打包。

附:maven构建的主要生命周期,它有以下23个阶段。

validateValidates whether project is correct and all necessary information is available to complete the build process.
initializeInitializes build state, for example set properties
generate-sourcesGenerate any source code to be included in compilation phase.
process-sourcesProcess the source code, for example, filter any value.
generate-resourcesGenerate resources to be included in the package.
process-resourcesCopy and process the resources into the destination directory, ready for packaging phase.
compileCompile the source code of the project.
process-classesPost-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes.
generate-test-sourcesGenerate any test source code to be included in compilation phase.
process-test-sourcesProcess the test source code, for example, filter any values.
test-compileCompile the test source code into the test destination directory.
process-test-classesProcess the generated files from test code file compilation.
testRun tests using a suitable unit testing framework(Junit is one).
prepare-packagePerform any operations necessary to prepare a package before the actual packaging.
packageTake the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file.
pre-integration-testPerform actions required before integration tests are executed. For example, setting up the required environment.
integration-testProcess and deploy the package if necessary into an environment where integration tests can be run.
pre-integration-testPerform actions required after integration tests have been executed. For example, cleaning up the environment.
verifyRun any check-ups to verify the package is valid and meets quality criterias.
installInstall the package into the local repository, which can be used as a dependency in other projects locally.
deployCopies the final package to the remote repository for sharing with other developers and projects.

There are few important concepts related to Maven Lifecycles which are wroth to mention:

  • When a phase is called via Maven command, for example mvn compile, only phases upto and including that phase will execute.

  • Different maven goals will be bound to different phases of Maven lifecycle depending upon the type of packaging (JAR / WAR / EAR).

相关推荐