[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

maven plugin for war file

Posted on 2014-08-14
13
Medium Priority
?
929 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

0
Comment
Question by:Rocking
  • 7
  • 6
13 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 40262087
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.
0
 

Author Comment

by:Rocking
ID: 40265218
how to build the war from maven by using the above plugin?
0
 
LVL 36

Expert Comment

by:mccarl
ID: 40266654
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Rocking
ID: 40266928
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.
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 40266952
Again, just as per the documentation (http://samaxes.github.io/minify-maven-plugin/minify-mojo.html) you can add a <cssSourceDir> element (and I guess the same for your js) to point to the correct folder. The doc says that the default is "css" so just add <cssSourceDir>WebContent/css</cssSourceDir> to the plugin's configuration in your pom.xml
0
 

Author Comment

by:Rocking
ID: 40266956
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?
0
 
LVL 36

Expert Comment

by:mccarl
ID: 40266963
Also on the very same page that I just linked to above...      <skipMerge>true</skipMerge>
0
 

Author Comment

by:Rocking
ID: 40266969
<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.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 40266986
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>
0
 

Author Comment

by:Rocking
ID: 40266992
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

0
 
LVL 36

Expert Comment

by:mccarl
ID: 40267009
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"
0
 

Author Comment

by:Rocking
ID: 40267054
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

0
 
LVL 36

Expert Comment

by:mccarl
ID: 40267508
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>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question