Showing posts with label Maven. Show all posts
Showing posts with label Maven. 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>

Friday, April 12, 2013

Maven - Copy Artifact in to a separate location

  • Create a Maven config file in the eclipse workspace.  maven_config.properties
  • Add the following key and value maven.output.build.path=/maven-dependency-plugin/target/test
  • Add the following two plugins to the Maven POM file.


   <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-dependency-plugin</artifactId>
    <version>2.7</version>
    <executions>
     <execution>
      <id>copy</id>
      <phase>package</phase>
      <goals>
       <goal>copy</goal>
      </goals>
      <configuration>
       <artifactItems>
        <artifactItem>
         <groupId>${project.groupId}</groupId>
         <artifactId>${project.artifactId}</artifactId>
         <version>${project.version}</version>
         <type>jar</type>
         <overWrite>true</overWrite>
         <outputDirectory>${maven.output.build.path}</outputDirectory>
        </artifactItem>
       </artifactItems>
      </configuration>
     </execution>
    </executions>
   </plugin>
  • Run the targets package or install

Friday, March 22, 2013

Useful Maven Plugins


  1. Apache Maven - Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. http://maven.apache.org/plugins/index.html
  2. Mojo - The Mojo project is a collection of plugins for Apache Maven 2 & 3 http://mojo.codehaus.org/plugins.html
  3. http://www.packtpub.com/article/useful-maven-plugins-part1
  4. http://nayidisha.com/techblog/useful-maven-plugins
  5. http://www.ibm.com/developerworks/java/library/j-5things13/index.html
  6. http://repository.ow2.org/nexus/content/sites/ow2-utilities/mergeproperties-maven-plugin/plugin-management.html
  7. http://evgeny-goldin.com/blog/maven-plugins-released/
Other Plugins
  • maven-replacer-plugin - https://code.google.com/p/maven-replacer-plugin
  • properties-maven-plugin

Friday, March 8, 2013

How to upload Artifact to the repository server

If you need to upload the artifact while you install to the local repository, follow the below steps.

1. Add the following setting in the setting.xml file which is in the local repository

    
      archiva.internal
      [UN]
      [PW]
    
  


2. In the POM file add the following settings

  
   archiva.internal
   Internal Release Repository
   [URL For the internal repository path]
  
 
Note : ID of the both files should be same

3. Run the following goal mv deploy

Please ensure upload rights is given in the relevant repository server

For exmple, if your using Apache Archiva then deployment user needs the Role 'Repository Manager' for each repository that you want to deploy.

Thursday, March 7, 2013

Default Maven plugins (Core Plugins)


A Plugin for executing external programs
mvn exec:exec

A plugin for compile and execute selected program with argumants
compile exec:java -Dexec.mainClass=camelinaction.FilePrinter -Dexec.args="100"


Delete the target folder and compile java classes
mvn clean compile


Deploys an arbitrary artifact to the JBoss application server
mvn jboss-as:deploy


First run the clean, build the project and install the artifact to the local repository
mvn clean install 


Executes the supplied java class in the current VM with the enclosing project's dependencies as classpath
mvn exec:java
   
    org.codehaus.mojo
    exec-maven-plugin
    1.1
    
     MyClass
    
   


Displays the dependency tree for the current project
mvn dependency:tree




Monday, March 4, 2013

Jboss AS 7 & Maven 3



  1. How to deploy the .war , .ear or .jar to the jboss AS7 while you package your project in eclipse 

  • In the eclipse Juno add the following parameter in the configuration option. Parameter Name  jboss-as.home and Value [Your jboss AS7 home path]
  • Add the following plug-in to the .pom file
  • Option 1
     
      
        org.jboss.as.plugins
        jboss-as-maven-plugin
        7.3.Final
        
         
          package
          
           deploy
          
         
        
       
    
  • Option 2
       
        org.jboss.as.plugins
        jboss-as-maven-plugin
        7.3.Final
       
      
In the clipse goals add the following 
jboss-as:deploy


Friday, October 5, 2012

Maven Build - invalid LOC header (bad signature)

if you are getting the "invalid LOC header (bad signature)" when building the maven project you need to identify the relevant packages and delete from the local repository which is in the  /home/[user]/.m2/repository/

ex. /home/kosala/.m2/repository/org/codehaus

Try to rebuild the project.