We help IT Professionals succeed at work.

maven plugin for war file

1,355 Views
Last Modified: 2014-08-22
hi,

I need to create a war file through maven plugin. It should compress/minify the css and js inside before making war to reduce the size of war.

I came across the solution but it is taking specific file,is there any way in whcih we can provide the js and css paths?

Example on the above would be helpful.


<build>
  <plugins>
    <plugin>
      <groupId>com.samaxes.maven</groupId>
      <artifactId>minify-maven-plugin</artifactId>
      <version>1.7.3</version>
      <executions>
        <execution>
          <id>default-minify</id>
          <configuration>
            <charset>UTF-8</charset>
            <cssSourceFiles>
              <cssSourceFile>file-1.css</cssSourceFile>
              <!-- ... -->
              <cssSourceFile>file-n.css</cssSourceFile>
            </cssSourceFiles>
            <jsSourceFiles>
              <jsSourceFile>file-1.js</jsSourceFile>
              <!-- ... -->
              <jsSourceFile>file-n.js</jsSourceFile>
            </jsSourceFiles>
            <jsEngine>CLOSURE</jsEngine>
          </configuration>
          <goals>
            <goal>minify</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Open in new window

Comment
Watch Question

mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
The above code that you posted appears to be the first example from the plugin's website. Did you check out the second example?
<project>
  <!-- ... -->
  <build>
    <plugins>
      <!-- ... -->
      <plugin>
        <groupId>com.samaxes.maven</groupId>
        <artifactId>minify-maven-plugin</artifactId>
        <version>1.7.3</version>
        <executions>
          <execution>
            <id>default-minify</id>
            <configuration>
              <cssSourceIncludes>
                <cssSourceInclude>**</cssSourceInclude>
              </cssSourceIncludes>
              <cssSourceExcludes>
                <cssSourceExclude>**/*.min.css</cssSourceExclude>
              </cssSourceExcludes>
              <jsSourceIncludes>
                <jsSourceInclude>**</jsSourceInclude>
              </jsSourceIncludes>
              <jsSourceExcludes>
                <jsSourceExclude>**/*.min.js</jsSourceExclude>
              </jsSourceExcludes>
            </configuration>
            <goals>
              <goal>minify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- ... -->
    </plugins>
  </build>
  <!-- ... -->
</project>

Open in new window

You can tailor the above patterns to correctly select all your required css, js files.

Author

Commented:
how to build the war from maven by using the above plugin?
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
That plugin isn't used to build the war, it is only used to minify (ie. compress) your css and js files before something else builds the war file.

So, as for building a war file you just set the <packaging> element to war and maven takes care of the rest. If you need to configure anything special though, you then might need to explicitly declare the maven-war-plugin so that you can set your configuration, but generally this is not necessary.

Author

Commented:
Execution default-minify of goal com.samaxes.maven:minify-maven-plugin:1.7.3:minify failed: basedir D:\workspace\study\mavenWar\src\main\webapp\css does not exist


how can i change the default path since i have put the css in "WebContent/css" folder.
IT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks for reply.

When i made the required modification suggested by you it shows he path like
 D:\workspace\study\mavenWar\src\main\webapp\WebContent\css

not removing webapp instead appending the source dir path to it.


i just want to minify the js and css file,as of now it is doing the same but also combining all the css into single and all js into single js.

how to avoid the combining of js & css into single?
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
Also on the very same page that I just linked to above...      <skipMerge>true</skipMerge>

Author

Commented:
<cssSourceDir>WebContent/css</cssSourceDir>


When i made the required modification suggested by you it shows the path like
 D:\workspace\study\mavenWar\src\main\webapp\WebContent\css

not removing webapp instead appending the source dir path to it.
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
not removing webapp instead appending the source dir path to it.
Well that's because I assumed that that was what you wanted. You weren't clear when you said "WebContent/css" at what level that that folder was at.

So, again, by just reading the documentation, remove the <cssSourceDir> configuration option that I mentioned earlier (because the default "css" will work fine now) and add....    <webappSourceDir>${basedir}/src/main/WebContent</webappSourceDir>

Author

Commented:
It's not working still checking in the same path (D:\workspace\study\mavenWar\src\main\webapp\WebContent\css)

POM is below

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
  <groupId>mavenWar</groupId>
  <artifactId>mavenWar</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>
  <name>test Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 <build>
    <plugins>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <executions>
            <execution>
                <id>default-war</id>
                <phase>prepare-package</phase>
                <configuration>
                <webappSourceDir>${basedir}/WebContent</webappSourceDir>
                <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <archiveClasses>false</archiveClasses>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix />
                        </manifest>
                        <manifestEntries>
                            <Implementation-Build>${build.number}</Implementation-Build>
                            <Implementation-Title>${project.name}</Implementation-Title>
                            <Built-By>${user.name}</Built-By>
                            <Built-OS>${os.name}</Built-OS>
                            <Build-Date>${build.date}</Build-Date>
                        </manifestEntries>
                    </archive>
                    <webResources>
                        <resource>
                            <!-- this is relative to the pom.xml directory -->
                            
                              <directory>${basedir}</directory>
                        </resource>
                    </webResources>
                </configuration>
            </execution>
        </executions>
    </plugin>
      <plugin>
        <groupId>com.samaxes.maven</groupId>
        <artifactId>minify-maven-plugin</artifactId>
        <version>1.7.3</version>
        <executions>
          <execution>
            <id>default-minify</id>
            <configuration>
             <skipMerge>true</skipMerge>
             <nosuffix>true</nosuffix>
              <cssSourceIncludes>
                <cssSourceInclude>**</cssSourceInclude>
              </cssSourceIncludes>
              <cssSourceExcludes>
                <cssSourceExclude>**/*.min.css</cssSourceExclude>
              </cssSourceExcludes>
              <jsSourceIncludes>
                <jsSourceInclude>**</jsSourceInclude>
              </jsSourceIncludes>
              <jsSourceExcludes>
                <jsSourceExclude>**/*.min.js</jsSourceExclude>
              </jsSourceExcludes>
            </configuration>
            <goals>
              <goal>minify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

Open in new window

mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
It's not working still checking in the same path
Are you sure? Because I would have expected it to be back looking in the original "src/main/webapp/css" path with that pom.xml

You've removed the <cssSourceDir> element (which is what I said to do above and is correct) but you haven't added the <webappSourceDir> element that I just told you to, so it will still be looking at "src/main/webapp/css"

Author

Commented:
not working

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
  <groupId>mavenWar</groupId>
  <artifactId>mavenWar</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>
  <name>test Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 <build>
    <plugins>
  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <executions>
            <execution>
                <id>default-war</id>
                <phase>prepare-package</phase>
                <configuration>
                <webappSourceDir>${basedir}/src/main/WebContent</webappSourceDir>
                <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <archiveClasses>false</archiveClasses>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix />
                        </manifest>
                        <manifestEntries>
                            <Implementation-Build>${build.number}</Implementation-Build>
                            <Implementation-Title>${project.name}</Implementation-Title>
                            <Built-By>${user.name}</Built-By>
                            <Built-OS>${os.name}</Built-OS>
                            <Build-Date>${build.date}</Build-Date>
                        </manifestEntries>
                    </archive>
                    <webResources>
                        <resource>
                            <!-- this is relative to the pom.xml directory -->
                            
                              <directory>${basedir}</directory>
                        </resource>
                    </webResources>
                </configuration>
            </execution>
        </executions>
    </plugin>
      <plugin>
        <groupId>com.samaxes.maven</groupId>
        <artifactId>minify-maven-plugin</artifactId>
        <version>1.7.3</version>
        <executions>
          <execution>
            <id>default-minify</id>
            <configuration>
             <skipMerge>true</skipMerge>
             <nosuffix>true</nosuffix>
              <cssSourceIncludes>
                <cssSourceInclude>**</cssSourceInclude>
              </cssSourceIncludes>
              <cssSourceExcludes>
                <cssSourceExclude>**/*.min.css</cssSourceExclude>
              </cssSourceExcludes>
              <jsSourceIncludes>
                <jsSourceInclude>**</jsSourceInclude>
              </jsSourceIncludes>
              <jsSourceExcludes>
                <jsSourceExclude>**/*.min.js</jsSourceExclude>
              </jsSourceExcludes>
            </configuration>
            <goals>
              <goal>minify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

Open in new window

mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
You've gone and put the <webappSourceDir> element in the "maven-war-plugin" configuration instead of in the "minify-maven-plugin" that we have been talking about.

And to save a bit of back and forth, because I am guessing that your next question will be how to get the "maven-war-plugin" to look at the right location too, funnily enough, it has documentation too... http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html    which if you have a look you will see that you can add a <warSourceDirectory> option to the "maven-war-plugin" configuration so that IT knows exactly where to look. You would give this the same value, so...

<warSourceDirectory>${basedir}/src/main/WebContent</warSourceDirectory>
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.