2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Way to minify Javascript or css file using maven plugin

I have javascript files and css file where i need to minify all the files to a single file like xyz.min.css and xyz.min.js at war file build time. How can i do so.

userimage

Please try below configuration in your pom.xml for getting minified file:


<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>test</groupId>
	<artifactId>test</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<properties>
		<java-version>1.8</java-version>
		<!-- your other dependency versions -->
	</properties>
	<dependencies>
		<!-- dependency jar artifacts -->
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java-version}</source>
					<target>${java-version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>com.samaxes.maven</groupId>
				<artifactId>minify-maven-plugin</artifactId>
				<version>1.7.6</version>
				<executions>
					<execution>
						<id>default-minify</id>
						<phase>package</phase>
						<configuration>
							<charset>UTF-8</charset>
							<cssSourceDir>/css</cssSourceDir> <!-- basically your css file path -->
							<cssSourceIncludes>
								<cssSourceInclude>**</cssSourceInclude>
							</cssSourceIncludes>
							<cssFinalFile>xyz.css</cssFinalFile>
							<jsSourceDir>/js</jsSourceDir> <!-- basically your js file path -->
							<jsSourceIncludes>
								<jsSourceInclude>**</jsSourceInclude>
							</jsSourceIncludes>
							<jsFinalFile>xyz.js</jsFinalFile>
							<jsEngine>CLOSURE</jsEngine>
						</configuration>
						<goals>
							<goal>minify</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
				</configuration>
			</plugin> -->
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<warSourceDirectory>webapp</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
					<webResources>
						<resource>
							<directory>src/main/webapp</directory>
						</resource>
					</webResources>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


Please do not forgot to add mvn package when you building  the war file. And maven build will generate two file like xyz.css and xyz.min.css. Now its up to you which one you want to use. Same for javascript too.

Answer is