user3188912
user3188912

Reputation:

Create jar file from maven

I want to create a jar file without SNAPSHOT cause when i used this command

mvn -f pom.XML package

created two files "file name_SNAPSHOT.jar and file name_SNAPSHOT_WITH_dependencies.jar" I want file name.jar i know that i should edit in pom.XML so i tried to add this

<plugin>
 <groupId>org.Apache.maven.plugins</groupId>  
   <artifactId>maven-source-plugin</artifactId>         
      <executions> 
       <execution>
        <id>attach-sources</id>
        <goals>
           <goal>jar</goal>
        </goals>
       </execution>
      </executions>
</plugin>

but it created file name_SNAPSHOT_source.jar how can i solve it ?!

My pom.XML like that

<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/xsd/maven-4.0.0.xsd">
  <model Version>4.0.0</model Version>
    <group Id>MyProject</group Id>
    <artifact Id>MyProject</artifact Id>
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging>

Upvotes: 2

Views: 8505

Answers (4)

MariuszS
MariuszS

Reputation: 31557

The proper way is to change project.version. But this is not recommended because development jar should has version with suffix -SNAPSHOT.

Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development.

More information:

Other solution without changing project.version is to use property finalName. This solution removes whole version from artifact.

<build>
 <finalName>${project.artifactId}</finalName>
</build>

After applying your pom.xml should look like this:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.something</groupId>
  <artifactId>myproject</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>My Project<name>
  <build>
     <finalName>${project.artifactId}</finalName>
  </build>
  ...
</project>

Property finalName apply also to Maven Assembly Plugin

Read also:

Upvotes: 6

Mikkel L&#248;kke
Mikkel L&#248;kke

Reputation: 3749

The Maven Release Plugin is your friend. It will:

  1. Update your pom file(s) so that they are no longer -SNAPSHOT
  2. Create a tag in your SCM denoting this release version
  3. Increment your pom file(s) to the next -SNAPSHOT version

Now all you have to do is checkout the version with the tag, for whichever version you want to build, and run mvn package to get your non-snapshot jar.

Alternatively you can of course do all this manually. :)

Upvotes: 0

Martin Seeler
Martin Seeler

Reputation: 6982

To create just the jar with project-name.jar and all dependencies included, try to use assembly plugin.

A configuration could look like this:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>{your.main.class}</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <outputDirectory>${project.basedir}</outputDirectory>
                <finalName>${project.artifactId}</finalName>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </execution>
    </executions>
</plugin>

Make sure to replace {your.main.class} with your fully qualified Main class. The jar will be generated in your project root folder instead of target.

Upvotes: 1

Moritz Petersen
Moritz Petersen

Reputation: 13047

You should definitively read the Maven documentation. However, the file name is derived from the project's artifact name and version. Your POM might look like this:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.something</groupId>
  <artifactId>myproject</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>My Project<name>

...

Your file name will be like: myproject-0.1-SNAPSHOT.jar.

The naming convention of Maven artifacts is: {artifact-name}-{artifact-version}.{artifact.packaging}.

In order to get rid of the SNAPSHOT part, just change the version to 0.1 or any other version you like.

For more information about snapshots, read this: What exactly is a Maven Snapshot and why do we need it?

Upvotes: 1

Related Questions