0

This project is a library, a framework that I'm working on and will be distributed as a jar file via mavenrepository.com.

I read this on the page under 1. Introduction.

The maven jar plugin provides the capability to build jars files, if you define that your project is packaged as a jar file, maven will call implicitly to this plugin. We don´t need to define it inside pom.xml it will be downloaded and executed when maven needs it.

The question is how do I define the project itself as a jar within pom.xml.

By default, I only want to have src/main/java files within the jar. src/test/java not required to be within the jar.

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
Developer
  • 924
  • 3
  • 14
  • 30
  • You shouldn't have to do anything since building jars is the default for Maven. Just note that the sources in `src/main/java` will not be added to the jar directly but are first compiled and then the compiled classes are added to the jar. If you want to explicitly set the packaging (which defines how the artifact will be bundled) you can add `jar` to your pom.xml (at "top level", i.e. as a direct child of the `` element). – Thomas Mar 19 '19 at 11:04
  • I'm getting bunch of `.class` files under target folder when I ran tests within IntelliJ but no jar files anywhere. Is there a config in pom.xml that specifies to create artifact under `project-dir/bin/Library.jar`, Earlier i used to define this within IntelliJ but ideally I think it should be in `pom.xml` – Developer Mar 19 '19 at 11:09
  • 1
    Maven will create the jar in the target folder by default. However it depends on whether the actual lifecycle phase has been executed - IntelliJ might not trigger it or at least not always (try `mvn package` or `mvn install` at the command line if Maven is on your path). Here's a question with a lot of answer regarding maven and its lifecyle (some nice images too): https://stackoverflow.com/questions/26607834/maven-lifecycle-vs-phase-vs-plugin-vs-goal – Thomas Mar 19 '19 at 11:14
  • I've installed `mvn`, added to the path, ran `mvn package`, I do see .jar file under within target folder. Is there some build automation available in pom.xml to have it placed under bin folder as well as a custom name? – Developer Mar 19 '19 at 11:28
  • 1
    Sure that's possible via some configuration (see https://stackoverflow.com/questions/6689511/how-to-place-the-output-jar-into-another-folder-with-maven and https://stackoverflow.com/questions/4238944/controlling-maven-final-name-of-jar-artifact). However, in many cases that's not necessary and might need additional configuration for further build steps. Why do you want/need to change the name and output location? – Thomas Mar 19 '19 at 11:38
  • Thanks, this answers the questions, happy to accept if you can write one. Although it's going to be distributed via mavenrepository.com (will ask another question for that), but the default layout of this library project is that they provide a custom jar file within bin directory as a standalone option as per their customs. target directory is under ignore list in .gitignore to avoid FilePollution, and only .jar is the of the main interest, that's why needed custom name and a custom output location. – Developer Mar 19 '19 at 11:48

2 Answers2

0

Put this tag in your project's pom.xml under <project> tag

    <packaging>jar</packaging>
Piotr K.
  • 1
  • 1
0

I'll summarize my comments as per request:

By default Maven will build a jar file in the project's target directory. That happens in the "package" lifecycle phase.

If you want to explicitly set the packaging to jar you can add the following as a child of your pom.xml's <project> element:

<packaging>jar</packaging>

For more information on the lifecycle see:

As is the case with almost everything in Maven the results can be customized with more or less effort. I'll not reiterate how it is done but provide links to questions on those topics:

Just note that because it is possible to change those via configuration you should have a good reason to do so. Every deviation from the convention (standard) will most likely result in the need for even more configuration.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • hoping you'd help me with this query, I was able to upload the jar to maven, I've also copied/placed the jar in the `bin` directory under github repo for distribution since it was a custom with other ports of this library. My question is, is it odd that I've placed jar file there or should I delete it and have users totally depend on maven distribution. – Developer Apr 02 '19 at 07:52
  • Basically the question has been circling my mind because of your comment -> `Why do you want/need to change the name and output location?` – Developer Apr 02 '19 at 08:06
  • Hmm, I'm not sure I fully understand the intended setup but I'd say that if you distribute your library on Maven Central anyways then the dependencies should be deployed there as well. If there are different ports for those dependencies that a classifiert might help, i.e. provide the standard version as the default dependency and let users override them via some profile settings, manual exclusion and inclusion etc. – Thomas Apr 02 '19 at 08:10
  • If your library is meant to be built manually then adding dependencies in the `bin` directory could be ok but if those aren't built as well then providing them via Maven seems to be the better way. – Thomas Apr 02 '19 at 08:14
  • let me clarify a bit, when i say port/languages, it mean same library in python or c or swift or actionscript etc. they didn't have a dependency service like maven in those earlier days so they end up distributing the binaries via github for manual inclusion. Another aspect is that this library is independent and complete in it's entirety meaning it doesn't have sub-dependencies. So it drills down to basically do even people in Java world require the jars for manual inclusions or everyone can be happy with maven central alone? – Developer Apr 02 '19 at 09:59
  • 1
    Well, I can't speak for the entire Java world but I'm happy with dependencies being available in Maven Central only. That also has the added benefit that the jars can be security audited by the community and thus it's easier to incorporate them into software that requires such an audit. If one can't use Maven Central directly for some reason there are at least 2 other options: 1) use your own repo manager like Nexus which gets missing dependencies from Maven Central and caches them 2) manually download the jars and add them to a local build. – Thomas Apr 02 '19 at 11:34