1

I would like to create single executable jar which will include all jars and required configuration files from maven. This is beter to do with maven-dependency-plugin. What jar files should I include? All files that are in lib folder that we have after installing maven?

How should I start it? Launcher class is org.codehaus.plexus.classworlds.launcher.Launcher?

Thank you.

yart
  • 7,515
  • 12
  • 37
  • 37
  • http://one-jar.sourceforge.net/?page=build-tools&file=maven-example – Zutty Apr 26 '13 at 09:40
  • @Zutty, the context of the question is not how to cerate single jar but how to create single jar including maven, so I can use it inside of this jar. Can you give example what I should include using one-jar? – yart Apr 26 '13 at 09:57
  • In accordance to this comment I deleted my answer, since I did the same mistake. I have no idea how to include maven, and I think thats a bad idea in the first place. Maven is a tool to create applications, I cannot see the need to include it anywhere. Do you rely on a maven plugin at runtime? – Scorpio Apr 26 '13 at 10:05
  • 1
    Why would you like to "include" maven and "use it" into a jar ? What is exactly the project you are trying to build with maven ? That's the problem. –  Apr 26 '13 at 10:20
  • @Magleff, we have project which is started through maven. We can start maven which is installed on machine but that means we need specify maven installation on the system as prerequisites. We could have cases when maven is not installed and it means better to have jar already with maven. Hope it answers your question. – yart Apr 26 '13 at 12:44
  • @Scorpio, we have already pom.xml which should be started by maven. – yart Apr 26 '13 at 12:46
  • 1
    Maven is not meant to launch a jar. Maybe you are currently using exec-maven-plugin to launch it, but you should rather use maven to BUILD the final executable jar. Even if you want to launch this jar on a server, maven should only help you to GENERATE a proper shell file to execute. Take a look to [this question](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven), it may help you. –  Apr 26 '13 at 13:16
  • https://stackoverflow.com/questions/35217128/is-it-possible-to-build-a-java-project-only-once-using-eclipse-and-share/35359756#35359756 – Thanga Sep 11 '17 at 08:16

2 Answers2

2

You have to add maven assembly plugin in your pom file it should be like this

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                      <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                      <manifest>
                        <mainClass>org.codehaus.plexus.classworlds.launcher.Launcher</mainClass>
                      </manifest>
                    </archive>
                  </configuration>
                <version>2.3</version>
            </plugin>

I guess you do have a main method define in your Launcher class, which will be entry point of your application. After this you have to pack your code with dependencies which is required to run your project. In terminal refer to your project home directory and build using

mvn assembly:assembly

that will generate two jar file use jar with dependency to run your application. Now run your jar using

java -jar your_jar_with_dependency.jar
Anurag Tripathi
  • 1,208
  • 1
  • 12
  • 31
  • Thank you for the answer but as mentioned I know how to create such jar file and what I need is to know the libraries I should include. Do you know what should be included and specified as dependency? – yart Apr 26 '13 at 12:48
  • It depends on your project what dependency you need. You have to include all of them in your pom if you are not sure remove dependencies one by on and try to run your project, that way you'll know what to keep and what to remove. – Anurag Tripathi Apr 26 '13 at 17:32
0

You can try using maven-dependency-plugin to get rid of unused dependencies. It will generate a html report on what are you declaring and using, what are you declaring and not using, what are you using and not declaring. It will only be useful to clear the ones you use in compile time, but it is a good place to start. Also, creating bulky jars is not always the best option, the idea behind maven is not having to pack all dependencies and distribute them, but to rely upon public repositories to download dependencies.

amaurs
  • 1,622
  • 1
  • 13
  • 18