1

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>
  • First can you show your full pom file apart from that change the version in your pom file into something like `1.0` which will produce a jar file without `-SNAPSHOT`. Furthermore you should name your pom file like `pom.xml` and than you can omit `mvn -f pom.XML` from your maven call. Based on what you mentioned it seemed to me you are using maven-assembly-plugin etc. which creates some file. – khmarbaise Jan 14 '14 at 07:52
  • sorry i can't understand you well but do you mean that version of maven caused that ? i named file pom.XML i have Apache Maven 2.2.1 (rdebian-8) –  Jan 14 '14 at 08:02
  • The snippet `0.0.1-SNAPSHOT` in your pom file is causing that you get created artifacts with `-SNAPSHOT`. The maven version (2.2.1) does not matter in that case. Change the name of your pom file from `pom.XML` into `pom.xml`. – khmarbaise Jan 14 '14 at 08:26
  • 1
    it's pom.xml but i wrote it unwitting in post XML –  Jan 14 '14 at 08:43

4 Answers4

5

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:

Community
  • 1
  • 1
MariuszS
  • 27,972
  • 12
  • 103
  • 144
  • Thanks, i'll try it now –  Jan 14 '14 at 08:38
  • i added this in pom ${project.artifactId} and got exception Caused by: org.codehaus.plexus.util.xml.pull.XmlPullParserException: Unrecognised tag: 'fileName' (position: START_TAG seen ...\n ... @12:15) –  Jan 14 '14 at 08:50
  • sorry i read it quickly .. many thanks it worked successfully i want to vote up but i got this "Vote Up requires 15 reputation" –  Jan 14 '14 at 09:02
  • @user3188912 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work :) Accept good answer – MariuszS Jan 14 '14 at 09:11
1

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?

Community
  • 1
  • 1
Moritz Petersen
  • 11,897
  • 2
  • 35
  • 42
  • Thanks,i edited in the post a part of my pom.XML ,so do you mean that i should change number in the tag to any number ? i'll read the link you sent now .many thanks –  Jan 14 '14 at 08:14
  • Yes, that's what I mean. BTW: If my answer(s) help, then you should accept them. – Moritz Petersen Jan 14 '14 at 08:15
  • I want to vote up but i got this "Vote Up requires 15 reputation" –  Jan 14 '14 at 08:18
1

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.

Martin Seeler
  • 6,475
  • 2
  • 29
  • 43
0

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. :)

Mikkel Løkke
  • 3,439
  • 20
  • 34