46

I created a new maven project in IntelliJ and set packaging to jar but when I build it, the target folder does not contain a jar. I'm sure its something really dumb on my part but there are just so many different things i'm reading on different websites and I just feel better asking. enter image description here

Joe
  • 1,954
  • 5
  • 16
  • 22
  • 6
    when you built it, did you click `Maven Projects > Life Cycle > package` ? – ZhongYu May 13 '15 at 03:22
  • @ZhongYu no, you see, this question is about using IntelliJ IDEA, so when he ***builds*** it, he does not click anything maven, because he is using IntelliJ IDEA, and he is building with IntelliJ IDEA. – Mike Nakis May 17 '20 at 16:03
  • Goto Maven Projects > Life Cycle > package then right click and from the second group choose when you want your package created. After that, whenever you Build it will remember that and automatically create your jar file. – Dan M May 30 '20 at 16:00

6 Answers6

45

You should build you project using IDEA's Maven Projects view.

View -> Tool Windows -> Maven Projects

or open it from left bottom corner menu:

menu

And then build your project with maven goals - i.e. package: maven project

If packaging is set to jar in pom.xml, you will get a jar in target dir.

Amr Lotfy
  • 2,937
  • 5
  • 36
  • 56
arghtype
  • 4,376
  • 11
  • 45
  • 60
  • I did that and it did create the jar just like I wanted to. The only thing is when I added it to the project structure of another project, I couldn't access the class from this library. I just added a SimpleUser class to test it but that wasn't showing up in my other project. – Joe May 13 '15 at 20:34
17

Go to the maven project and double click clean and package.

For just do following:

enter image description here

ankit
  • 2,591
  • 2
  • 29
  • 54
15

You need the maven jar plugin in order to create a jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>add your main class</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

https://maven.apache.org/plugins/maven-jar-plugin/

sethu
  • 8,181
  • 7
  • 39
  • 65
5

You need to have "jar" in packaging section of your pom.xml. You can add this before dependencies section:

<packaging>jar</packaging>

Then go to View -> Tool Windows -> Maven and in Maven window expand everything and double click on "package" in Lifecycle section.

Alec
  • 51
  • 1
  • 4
1

Assuming that the screen-shot shows the complete pom file, you are missing the entries that define the artifact. Try adding something like this following immediately after the tag:

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.example</groupId>
  <artifactId>stackoverflow-question</artifactId>
  <version>0.0.1-SNAPSHOT</version>

You should end up with stackoverflow-question-0.0.1-SNAPSHOT.jar in your /target directory. You may need to refresh the directory to see it (You certainly have to in Eclipse)

kiwiron
  • 1,677
  • 11
  • 17
0

You can use iu, this is my way

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Dave Rincon
  • 308
  • 3
  • 10