Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Maven profiles to switch between minified/uncompressed JS?

I'm working on a Java web app that uses Maven for its build process, and has a lot of JavaScript.

What I want to do is basically have this in my JSP file:

<c:choose>
    <c:when test="PRODUCTION">
        <script type="text/javascript" src="/path/to/app.min.js"></script>
    </c:when>
    <c:otherwise>
        <script type="text/javascript" src="/path/to/script1.js"></script>
        <script type="text/javascript" src="/path/to/script2.js"></script>
    </c:otherwise
</c:choose>

Is this possible to do with Maven profiles?

like image 271
Joe Attardi Avatar asked Feb 12 '23 18:02

Joe Attardi


2 Answers

Have a look at maven resource filtering. You can use that to overwrite properties in your resource files with their actual values.

1) Use a property file which holds the environment name. say environment.properties which has the below content.

environment.name=${environment.name}

Now use the below in your pom file

<properties>
    <environment.name>TEST</environment.name>
</properties>

<profiles>
    <profile>
        <id>PROD</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <environment.name>PROD</environment.name>
        </properties>
    </profile>
</profiles>

and specify resource filtering in your pom

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>

if you run a build using this profile,

mvn clean install -P PROD

the environment.properties will be processed to

environment.name=PROD

and if you don't use this profile

mvn clean install

it will be processed to

environment.name=TEST

Now at runtime, read the environment.name property from environment.properties and use the appropriate .js file based on the property value.

OR

2) In case you don't want to read from the property file, you can filter the jsp itself.

<properties>
    <js.suffix></js.suffix>
</properties>

<profiles>
    <profile>
        <id>PROD</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <js.suffix>min.</js.suffix>
        </properties>
    </profile>
</profiles>

and set up the web resource filtering

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <webResources>
                    <resource>
                        <directory>src/main/webapp</directory>
                        <filtering>true</filtering>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

and add something of this sort in your jsp

 <script type="text/javascript" src="/path/to/app.${js.suffix}js"></script>

(Assuming that the uncompressed one is app.js and the compressed one is app.min.js)

like image 101
coderplus Avatar answered Apr 26 '23 19:04

coderplus


My solution was a bit different. I used the minify-maven-plugin and in development I use it to create a merged, but not minified app.js and when I'm in production, I use it to create a minified file. I named both of them app.js so I didn't have to change my JSP files with Maven.

For example:

<!-- Resource minification -->
<plugin>
  <groupId>com.samaxes.maven</groupId>
  <artifactId>minify-maven-plugin</artifactId>
  <version>1.7.2</version>
  <executions>
    <!-- JavaScript -->
    <execution>
      <id>minify-js</id>
          <phase>generate-sources</phase>
      <configuration>
        <charset>utf-8</charset>
        <skipMerge>false</skipMerge>
        <skipMinify>${javascript.skipMinify}</skipMinify>
        <nosuffix>true</nosuffix>
        <webappSourceDir>${project.basedir}/src/main/webapp</webappSourceDir>
        <jsSourceDir>assets/js</jsSourceDir>
        <jsSourceIncludes>
          <jsSourceInclude>*.js</jsSourceInclude>
        </jsSourceIncludes>
        <jsFinalFile>app.js</jsFinalFile>
      </configuration>
      <goals>
        <goal>minify</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Then I use Maven profiles to switch the ${javascript.skipMinify} property, for example:

<profile>
  <id>development</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <properties>
    <javascript.skipMinify>true</javascript.skipMinify>
  </properties>
</profile>
<profile>
  <id>production</id>
  <properties>
    <javascript.skipMinify>false</javascript.skipMinify>
  </properties>
</profile>
like image 28
g00glen00b Avatar answered Apr 26 '23 19:04

g00glen00b