switch min.js from development to production

we usually need the full js for development purpose, but the min.js for production.

same thing applies to CSS.

The way we are using is thru maven-replacer-plugin

<!-- DEVELOPMENT -->
<script src="lib/lib1.js"></script>
<script src="lib/lib2.js"></script>
<script src="js/js1.js"></script>
<script src="js/js2.js"></script>
<!-- /DEVELOPMENT -->
<!-- PRODUCTION
<script src="lib/lib1.min.js"></script>
<script src="//cdn/lib2.min.js"></script>
<script src="js/combined.min.js"></script>
/PRODUCTION -->

replacer plugin config:

The phase here need to the prepare-package. If you use package, the build will pass BUT only the file in the exploded will be replaced, the file in the war will not.

       			         <!--replace plugin so that we could switch our JS from development to production min.js. we do this before maven package the exploded to
             the war(prepare-package) phase. REF-> https://code.google.com/p/maven-replacer-plugin/wiki/UsageWithOtherPlugins-->
         <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>${maven.replacer.version}</version>
            <executions>
               <execution>
                  <phase>prepare-package</phase>
                  <goals>
                     <goal>replace</goal>
                  </goals>
               </execution>
            </executions>
            <configuration>
               <file>${project.build.directory}/${project.build.finalName}/WEB-INF/jsp/inc/angular-bootstrap-inc.jsp</file>
               <replacements>
                  <replacement>
                     <token>&lt;%-- DEVELOPMENT --%&gt;</token>
                     <value>&lt;%-- DEVELOPMENT</value>
                  </replacement>                  <replacement>
                     <token>&lt;%-- /DEVELOPMENT --%&gt;</token>
                     <value> /DEVELOPMENT --%&gt;</value>
                  </replacement>
                  <replacement>
                     <token>&lt;%-- PRODUCTION</token>
                     <value>&lt;%-- PRODUCTION --%&gt;</value>
                  </replacement>                  <replacement>
                     <token>/PRODUCTION --%&gt;</token>
                     <value>&lt;%-- /PRODUCTION --%&gt;</value>
                  </replacement>
               </replacements>
            </configuration>
         </plugin>

After the prod profile build:

<!-- DEVELOPMENT
<script src="lib/lib1.js"></script>
<script src="lib/lib2.js"></script>
<script src="js/js1.js"></script>
<script src="js/js2.js"></script>
/DEVELOPMENT -->
<!-- PRODUCTION -->
<script src="lib/lib1.min.js"></script>
<script src="//cdn/lib2.min.js"></script>
<script src="js/combined.min.js"></script>
<!-- /PRODUCTION -->

Maven war plugin config

I want to keep the local copy as development mode, so i basically replace the development with production when using the normal profile.

note the ‘useCache’, it is important according to HERE when we use plugin version 2.1.0+. I use 2.5 for war-plugin

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>${war.plugin.version}</version>
            <executions>
               <!--add this phase so that we can replace the development js with production min.js. Without this, the replacer cannot find file to work on.-->
               <execution>
                  <id>prepare-war</id>
                  <phase>prepare-package</phase>
                  <goals>
                     <goal>exploded</goal>
                  </goals>
               </execution>
               <execution>
                  <id>default-war</id>
                  <phase>package</phase>
                  <goals>
                     <goal>war</goal>
                  </goals>
                  <configuration>
                     <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
                  </configuration>
               </execution>
            </executions>
				<configuration>
					<warName>snap</warName>
					<archive>
						<manifest>
							<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
						</manifest>
						<manifestEntries>
							<Implementation-Build>${maven.build.timestamp}</Implementation-Build>
						</manifestEntries>
					</archive>
					<webResources>
						<resource>
							<filtering>true</filtering>
							<directory>src/main/webapp</directory>
							<includes>
								<include>**/web.xml</include>
							</includes>
						</resource>
					</webResources>
					<!-- Don't exclude anything (i.e. include the simulation page) when 
						doing a local build. -->
					<warSourceExcludes>**/loginSimulation.jsp</warSourceExcludes>
               <!-- version 2.1.0+ copies resources twice which overrides replacements, we use this config to 'fix' this.-->
               <useCache>true</useCache>
            </configuration>
			</plugin>

When it comes to local profile:

               <!--in local build, we want to keep the develop js, so ignore the replace plugin. -->
               <plugin>
                  <groupId>com.google.code.maven-replacer-plugin</groupId>
                  <artifactId>replacer</artifactId>
                  <version>${maven.replacer.version}</version>
                  <executions>
                     <execution>
                        <phase>none</phase>
                        <goals>
                           <goal>replace</goal>
                        </goals>
                     </execution>
                  </executions>
               </plugin>

Hope this helps. Reference here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s