Friday, August 5, 2011

Creating a Jar file within a web application in Maven.

In Maven typically when you create a project, you have to use a particular archetype so that the project packaging gets defined as a Jar or War (or some other) file.

Now in case if you are building a web application, you will use maven artifact archetype as "archetypeArtifactId=maven-archetype-webapp". This is going to create your application directory structure as Web application and packaging is going to be War.

When you build your web applicaiton using command "mvn install", it will create a War file of your web application. Now, there seldom can be a need where you may need a Jar file for the Java classes that you created in your web application.

You could do this by using additional maven plugins in your pom.xml file. The Jar plugin gets executed in the "compile" phase of the life-cycle. Once the Jar file is built you will also have to install it in the local repository using the install plugin, if some other project has a dependency on it and wants to use it.

Here is the build section in my pom.xml file which take care of creating a Jar file and installing it in the local repository -
<build>
  <finalName>server</finalName>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.3.1</version>
      <executions>
        <execution>
          <id>make-a-jar</id>
          <phase>compile</phase>
          <goals>
            <goal>jar</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-install-plugin</artifactId>
      <version>2.3.1</version>
      <executions>
        <execution>
          <id>install-a-jar</id>
          <phase>install</phase>
          <goals>
            <goal>install-file</goal>
          </goals>
          <configuration>
            <groupId>com.my-company.server</groupId>
            <artifactId>server</artifactId>
            <packaging>jar</packaging>
            <version>1.0-SNAPSHOT</version>
            <file>target/server.jar</file>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

1 comment:

  1. Jackpot City Hotel & Casino | Official Website | JetBlue.com
    Make your Vegas trip more affordable and 충청북도 출장마사지 fun! Book a charter bus, limo, or 부산광역 출장마사지 car to Jackpot 천안 출장안마 City Hotel & 공주 출장샵 Casino 상주 출장마사지 in Houston.

    ReplyDelete