Showing posts with label Maven Plugins. Show all posts
Showing posts with label Maven Plugins. Show all posts

Tuesday, March 4, 2014

Maven - Deploy war in Tomcat 7 & 8

This article will explain on how to deploy a war fine in to Tomcat 7 through maven build.

Note : I have tested same settings for Tomcat 8 as well. So below settings can be apply for both versions.

  • Configure tomcat7-maven-plugin in pom.xml
   <!-- Tomcat plugin -->
   <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
     <path>/${project.build.finalName}</path>
     <update>true</update>
     <url>http:// localhost:8080/manager/text</url>
     <username>tomcat</username>
     <password>tomcatuser</password>
    </configuration>
   </plugin>

  • Configure user rights in the tomcat-user.xml which is in the tomcat conf file.
   <role rolename="tomcat" />
   <role rolename="manager-gui" />
   <role rolename="manager-script" />
   <role rolename="admin-gui" />
   <user username="tomcat" password="tomcatuser"
    roles="tomcat,manager-gui,admin-gui,manager-script" />
  •  Once above steps are followed, You can deploy the war by running following maven goal
mvn tomcat7:redeploy

More details of the Apache Tomcat Maven Plugin refer maven-plugin-2.2


Thursday, August 22, 2013

Maven - How to merge files


You can use maven-merge-properties-plugin to merger property files.

<plugin>
    <groupId>org.beardedgeeks</groupId>
    <artifactId>maven-merge-properties-plugin</artifactId>
    <version>0.1.1</version>
    <configuration>
     <merges>
      <merge>
       <targetFile>${Target file name with the src path}</targetFile>
       <propertiesFiles>
        <propertiesFile>${property file 1 with the src path}</propertiesFile>
        <propertiesFile>${property file 2 with the src path}</propertiesFile>
        <propertiesFile>${property file 3 with the src path}</propertiesFile>
        <propertiesFile>${property file 4 with the src path}</propertiesFile>
       </propertiesFiles>
      </merge>          
     </merges>
    </configuration>
    <executions>
     <execution>
      <phase>compile</phase>
      <goals>
       <goal>merge</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

Thursday, July 4, 2013

Maven - How to restrict files when build the project

You can add the below code next to the <build> tag.

  <resources>
   <resource>
    <directory>${src path which files to be ignored}</directory>
    <includes>
     <include>${file1 name}</include>
     <include>${file2 name}</include>
    </includes>
    <targetPath>${target path}</targetPath>
   </resource>
  </resources>

Wednesday, April 17, 2013

Maven - How to Replace a Token with the value

If you need to replace the Token with the value in a jar you can use the below plugins. This example would replace the token for all the files which is given in a particular location.

 <build>
  <plugins>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
     <execution>
      <phase>initialize</phase>       
       <goals>
       <goal>read-project-properties</goal>
      </goals>
      <configuration>
       <files>
        <file>../../maven_config.properties</file>
       </files>
      </configuration>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <executions>
     <execution>
      <id>prepare-jar</id>
      <phase>clean</phase>
      <goals>
       <goal>jar</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.2</version>
    <executions>
     <execution>
      <phase>compile</phase>
      <goals>
       <goal>replace</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <includes>
      <include>target/classes/**/*.xml</include>
      <include>target/classes/**/*.properties</include>
     </includes>
     <token>@@portalname@@</token>
     <value>${portal.name}</value>
    </configuration>
   </plugin>
  </plugins>
 </build>

If you need to replace the Token with the value in a war you can use the below plugins. Below example would illustrate how to replace the token and value in a one file.


 <build>
  <plugins>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
     <execution>
      <goals>
       <goal>read-project-properties</goal>
      </goals>
      <configuration>
       <files>
        <file>../../maven_config.properties</file>
       </files>
      </configuration>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.0.1</version>
    <executions>
     <execution>
      <id>prepare-war</id>
      <phase>prepare-package</phase>
      <goals>
       <goal>exploded</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.2</version>
    <executions>
     <execution>
      <phase>prepare-package</phase>
      <goals>
       <goal>replace</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <file>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/web.xml</file>
     <replacements>
      <replacement>
       <token>@@portalname@@</token>
       <value>${portal.name}</value>
      </replacement>
     </replacements>
    </configuration>
   </plugin>
  </plugins>
 </build>