Maven 2 uses a standard directory layout for projects, documented here:
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
What I'm wondering is: are there recommended conventions for other projects besides the plain-vanilla Java library, Java EE and WebApp projects? Specifically, where to place ant files, start scripts, configuration files, sample applications, etc.
In addition, what is the convention for placing files outside of the src/ directory tree? For example, is it common to place documentation under doc/ (as sibling of src)?
Is there a page where these conventions are compiled? If not, can other readers provide examples from their own projects?
The main folder I place extra config files in is under src/main/java/resources. Usually I created sub directories under there. The tests can have their own config files src/test/resources.
You can use directives in the build section of the pom.xml to specify additional resources directories and where to copy files to specific places in the target directory. Usually a convention arises for the language or framework you are trying to use. In which case the mess can be hidden in a parent pom.xml
See http://maven.apache.org/pom.html#Resources
I don't think there is an official layout for desktop applications, but this is the layout we use for ours.
src/main/config - Config files, copied and filtered to ${project.build.directory}/config.
src/main/scripts - sh, bat, README and other plain-text files that are copied and filtered to ${project.build.directory}.
src/main/bin - Binary files that are not filtered and copied to ${project.build.directory}.
src/main/build - Any additional scripts, ant files, or resources used by the build process but not included in the application.
src/main/assembly - Assembly descriptor to create application assembly.
Copy dependencies to ${project.build.directory}/lib using maven-dependency plugin.
Bonus points if you use a shared assembly descriptor jar which includes the config and lib directories along with standard file types to share with all your projects. A good set of default file types to include is .sh, .bat, and .exe with executable permissions; .jar, .zip, .txt, .pdf, .xml, .properties, .conf, .ico, .png, .jpg with standard permissions.
Add the config directory and lib/ prefix to the classpath using the maven-jar plugin to make a runnable jar.
Add a installer profile that builds an assembly of your application using the maven-assembly plugin and your assembly descriptor. This assembly can then be consumed by a separate installer project if needed.
Avoid putting anything else at the src/ level. Instead put documentation inside the src/site/ directory (eg. src/site/sphinx) or src/doc if you really need to.
Put all the above configuration in a parent/corporate pom to share with all your projects. Simply reference the maven-dependency, maven-resources, maven-jar, and maven-assembly plugins in you project to build an entire application with almost no configuration (don't forget to set the main-class for the maven-jar plugin).
Related
I am reading Maven documentation and came across the name uber-jar.
What does an uber-jar mean and what are its features/advantages?
Über is the German word for above or over (it's actually cognate with the English over).
Hence, in this context, an uber-jar is an "over-jar", one level up from a simple JAR (a), defined as one that contains both your package and all its dependencies in one single JAR file. The name can be thought to come from the same stable as ultrageek, superman, hyperspace, and metadata, which all have similar meanings of "beyond the normal".
The advantage is that you can distribute your uber-jar and not care at all whether or not dependencies are installed at the destination, as your uber-jar actually has no dependencies.
All the dependencies of your own stuff within the uber-jar are also within that uber-jar. As are all dependencies of those dependencies. And so on.
(a) I probably shouldn't have to explain what a JAR is to a Java developer but I'll include it for completeness. It's a Java archive, basically a single file that typically contains a number of Java class files along with associated metadata and resources.
ubar jar is also known as fat jar i.e. jar with dependencies.
There are three common methods for constructing an uber jar:
Unshaded: Unpack all JAR files, then repack them into a single JAR.
Works with Java's default class loader. Tools maven-assembly-plugin
Shaded: Same as unshaded, but rename (i.e., "shade") all packages of all dependencies. Works with Java's default class loader. Avoids some (not all) dependency version clashes. Tools maven-shade-plugin
JAR of JARs: The final JAR file contains the other JAR files embedded within. Avoids dependency version clashes. All resource files are preserved. Tools: Eclipse JAR File Exporter
for more
Paxdiablo's definition is really good.
In addition, please consider delivering an uber-jar is sometimes quite useful, if you really want to distribute a software and don't want customer to download dependencies by themselves. As a draw back, if their own policy don't allow usage of some library, or if they have to bind some extra-components (slf4j, system compliant libs, arch specialiez libs, ...) this will probably increase difficulties for them.
You can perform that :
basically with maven-assembly-plugin
a bit more further with maven-shade-plugin
A cleaner solution is to provide their library separately; maven-shade-plugin has preconfigured descriptor for that. This is not more complicated to do (with maven and its plugin).
Finally, a really good solution is to use an OSGI Bundle. There is plenty of good tutorials on that :)
For further configuration, please read those topics :
Should you provide dependent libraries in client jar?
Best practices in building and deploying Clojure applications: good tutorials?
The different names are just ways of packaging java apps.
Skinny – Contains ONLY the bits you literally type into your code editor, and NOTHING else.
Thin – Contains all of the above PLUS the app’s direct dependencies of your app (db drivers, utility libraries, etc).
Hollow – The inverse of Thin – Contains only the bits needed to run your app but does NOT contain the app itself. Basically a pre-packaged “app server” to which you can later deploy your app, in the same style as traditional Java EE app servers, but with important differences.
Fat/Uber – Contains the bit you literally write yourself PLUS the direct dependencies of your app PLUS the bits needed to run your app “on its own”.
Source: Article from Dzone
Reposted from: https://stackoverflow.com/a/57592130/9470346
A self-contained, executable Java archive. In the case of WildFly Swarm uberjars, it is a single .jar file containing your application, the portions of WildFly required to support it, an internal Maven repository of dependencies, plus a shim to bootstrap it all. see this
According to uber-JAR Documentation Approaches:
There are three common methods for constructing an uber-JAR:
Unshaded Unpack all JAR files, then repack them into a single JAR.
Tools: Maven Assembly Plugin, Classworlds Uberjar
Shaded Same as unshaded, but rename (i.e., "shade") all packages of all dependencies.
Tools: Maven Shade Plugin
JAR of JARs The final JAR file contains the other JAR files embedded within.
Tools: Eclipse JAR File Exporter, One-JAR.
For Java Developers who use SpringBoot, ÜBER/FAT JAR is normally the final result of the package phase of maven (or build task if you use gradle).
Inside the Fat JAR one can find a META-INF directory inside which the MANIFEST.MF file lives with all the info regarding the Main class. More importantly, at the same level of META-INF directory you find the BOOT-INF directory inside which the directory lib lives and contains all the .jar files that are the dependencies of your application.
I use standard maven directory structure for my project http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
However, this is problematic because configuration files are going to be in jar file. There is some discussion on reading the config files from predefined location as How to get the path of a running JAR file?
But I am trying to package my jar file without any configuration files in it and make the directory structure look similar to any other open source projects that we download
project|
--lib
|
--conf
|
--docs
|
--src
I prefer not to do any source code changes (ex:all my config files are in source path for maven development and I just read the files directly without prepending with the path); if I have to change path to config files in source code, what should I do to make the path work both in development and in production
My questions
1)how can I have current "development" directory structure as is, but have my "production" release directory will look similar to that of any apache(say) project
2)for every release, what changes should I do in pom.xml so that the version numbers are appended to jar and incremented
I prefer not to do any source/java code changes (ex:all my config files are in source path for maven development and I just read the files directly without prepending with the path); if I have to change path to config files in source code, what should I do to make the path work both in development and in production
(I am using Maven and Eclipse)
In my projects I use the maven assembly plugin to package the application in a redistribuable way :
Everything under src/main/java and src/main/resources is packaged inside the main jar (standard behaviour).
Then using assembly plugin, I create a zip containing 3 folders :
/lib : contains the main jar + all the dependencies
/bin : contains the additional files from the src/main/scripts maven source folder (startup shell scripts)
/etc (or /conf) : contains the additional files from the src/main/config maven source folder (config files)
I find this setup very convenient as it is really close to the initial maven standard layout (src/main/java + src/main/resources in the main jar and other folders taken appart), still being adapted to my projects.
You can use your existing resources by mentioning those resource directories in Maven Resource Plugin.
add following code to build ( tag )section of your pom.xml
<resources>
<resource>
<directory>[your folder here]</directory>
</resource>
</resources>
you can add several non maven standard resources over here ( anything other that src/main/resources ).
I want to share some more tips based on what I digged out (#superbob gave good explanation).
This is easier said than done....
I wanted to create jar (executable first) and copy this jar file into zip (along with few other directories for releasing in to production)...it is easy if u can look at existing pom file than fiddling with existing pom.
Look here for an existing project(pom.xml) http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnable-binary-distribution-with-maven-assembly-plugin/
In my case, I had multiple pom files and it adds another layer of complexity for newbie like me..look here for multi module example with maven assembly plugin http://maven.apache.org/plugins/maven-assembly-plugin/examples/multimodule/index.html
Last but not least, run the following command to make sure assembly is created
mvn clean install package assembly:single
I guess this is a two-part question. I am trying to write my own Ant task (MyFirstTask) that can be used in other project's build.xml buildfiles. To do this, I need to compile and package my Ant task inside its own JAR. Because this Ant task that I have written is fairly complicated, it has about 20 dependencies (other JAR files), such as using XStream for OX-mapping, Guice for DI, etc.
I am currently writing the package task in the build.xml file inside the MyFirstTask project (the buildfile that will package myfirsttask.jar, which is the reusable Ant task).
I am suddenly realizing that I don't fully understand the intention of a Java JAR. Is it that a JAR should not contain dependencies, and leave it to the runtime configuration (the app container, the runtime environment, etc.) to supply it with the dependencies it needs? I would assume if this is the case, an executable JAR is an exception to the rule, yes?
Or, is it the intention for Java JARs to also include their dependencies?
Either way, I don't want to be forcing my users to be copying-n-pasting 25+ JARs into their Ant libs; that's just cruel. I like the way WAR files are set up, where the classpath for dependencies is defined under the classes/ directory.
I guess, ultimately, I'd like my JAR structure to look like:
myfirsttask.jar/
com/ --> the root package of my compiled binaries
config/ --> config files, XML, XSD, etc.
classes/ --> all dependencies, guice-3.0.jar, xstream-1.4.3.jar, etc.
META-INF/
MANIFEST.MF
I assume that in order to accomplish this (and get the runtime classpath to also look into the classes/ directory), I'll need to modify the MANIFEST.MF somehow (I know there's a manifest attribute called ClassPath, I believe?). I'm just having a tough time putting everything together, and have a looming/lingering question about the very intent of JARs to begin with.
Can someone please confirm whether Oracle intends for JARs to contain their dependencies or not? And, either way, what I would have to do in the manifest (or anywhere else) to make sure that, at runtime, the classpath can find the dependencies stored under the classes/ directory? Thanks in advance!
The term 'JAR file' can mean at least two things, or rather, has at least two facets to its meaning. Most basically, it means a container format: basically, a ZIP file with a META-INF directory. More refinedly, it means this container used as a way to package class files.
In the sense of being a container, there is no intent with respect to contents; the file could contain class files, other JARs (in either sense!), etc. But in the sense of being a packaging of code, i believe the intent for JAR files proper is for them not to contain any dependencies.
If you have a read of the JAR File Specification, you'll find there are several allusions to the storage of class files, but nothing about storing other JAR files. Correspondingly, if you look at the implementation of the JAR file classloader in the JRE, it can't do anything useful with nested JARs.
Furthermore, the JAR specification does detail a mechanism for dealing with non-nested dependencies: the Class-Path attribute. This lets a JAR file make relative references to other JAR files in the filesystem.
Now, in-the-sense-of-a-packaging JAR files are not the only use of in-the-sense-of-a-container JAR files. WAR, EAR, and RAR files (and more besides) are all JAR files used for particular purposes. Each of those is capable of containing other JARs: WARs can contain in-the-sense-of-a-packaging JAR files, and EARs can contain those and also WARs. However, those are quite different beasts than in-the-sense-of-a-packaging JAR files. It's worth noting that special classloaders, that are not in the Java standard library, are needed to make use of them.
The way that WARs etc can collect many JAR files together is indeed very useful, and it's a real shame there's no generic mechanism for doing this in Java outside of Java EE. It would be great to have an 'application archive' or 'meta-archive' format that simply bundled some JARs.
So, you're left with this problem of users needing 25 JARs in order to use your plugin. You have roughly two options.
First, you accept the pain, and distribute your plugin as a zip full of JARs, which users will have to unpack.
Secondly, you join the 21st century, and use a build tool and distribution mechanism which handles dependencies automatically: in practice, that means using Gradle, or Maven, or some other tool (such as Ant) in concert with Ivy, to obtain dependencies from Maven Central, and then then releasing your code along with a POM file which lists those dependencies. Users can then download your JAR and your POM, and have their own build tool obtain the dependencies.
If you do go the second route, it might be prudent to also release a zip of the dependencies, for the benefit of users who are not using automatic dependency management.
The intent (AFAIU) is for JAR files to behave like native code shared object files (.so on Unix, .dll on Windows). Generally, an application will install several shared object files as siblings, plus an executable with which to launch them.
An executable JAR is more like a standalone executable, and so it is more common to include all dependencies (similar to the way a statically-linked native code executable contains all its dependent objects directly).
Unfortunately, the default ClassLoader is not able to load classes from nested JARs. It is possible to write a ClassLoader that does. Or you can use one someone else has written. From the description of your problem, it sounds like Jar Jar Links is exactly what you're looking for.
Is it correct or incorrect for a Java JAR to contain its own dependencies?
There are use cases where it is correct for a JAR file to contain its own
dependencies. If you would like to support users who do not use modern
dependency management, you may want to provide a JAR file containing your Ant
task code as well as all dependencies. The more powerful, flexible, and modular
approach is to publish versioned JAR files to the Maven repository that
only contain your project code.
1) JAR file containing your project code and all dependencies
Pros
Easy to download and the only setup for end users is including a
<taskdef> in their Ant build files
No setup required to publish Maven artificats
Example Ant target to build JAR
<target name="jar" depends="compile"
description="Creates a standalone JAR of all class files and dependencies.">
<jar destfile="${my.ant.task.jar.file}" update="true">
<fileset dir="${build.classes.dir}" />
<zipfileset src="${lib.dir}/javax.inject.jar" />
<zipfileset src="${lib.dir}/guice-3.0.jar" />
<zipfileset src="${lib.dir}/guice-multibindings-3.0.jar" />
<zipfileset src="${lib.dir}/guice-assistedinject-3.0.jar" />
</jar>
</target>
Cons
If end users of your Ant task already have some or all of the dependencies
included in their projects, then they will end up with redundant copies of the
dependencies
The JAR file could be very large
2) JAR file containing only your project code published to Maven Repository
Pros
Users may fetch any version of your Ant task that you have published to
the Maven repository, which provides more flexibility in releasing new
versions of your task while allowing existing users to continue using previous
versions to avoid possible regressions
Avoids duplicate copies of common dependencies (except where different versions of a dependency cause errors)
JAR file will be small
Cons
Need to learn about the following:
Maven Repository
Publishing Maven Artifacts - see
Ant + Ivy specific preparations and procedures
Apache Ivy - Dependency manager integrated with Apache Ant
For reference, the Java™ Tutorials provide a good summary of JAR files.
Lesson: Packaging Programs in JAR Files
The Java™ Archive (JAR) file format enables you to bundle multiple files
into a single archive file. Typically a JAR file contains the class files and
auxiliary resources associated with... applications.
The JAR file format provides many benefits:
Security: You can digitally sign the contents of a JAR file...
Decreased download time: If your applet is bundled in a JAR...
Compression: The JAR format allows you to compress your files for efficient
storage.
Packaging for extensions: The extensions framework provides a means by
which you can add functionality to the Java core platform, and the JAR file
format defines the packaging for extensions...
Package Sealing: Packages stored in JAR files can be optionally sealed so
that the package can enforce version consistency. Sealing a package within a
JAR file means that all classes defined in that package must be found in the
same JAR file.
Package Versioning: A JAR file can hold data about the files it contains,
such as vendor and version information.
Portability: The mechanism for handling JAR files is a standard part of the
Java platform's core API.
"Jar Jar Links" is only good for a standalone applications. But not for Ant.
If your project has the same dependencies and they are upgraded to newer versions later on, like xstream-*.jar, then there will be a conflict, and the wrong version may be picked up. In the worst case there will be MethodNotFoundException. That's why it is a bad practice to include dependencies in a single jar.
What's a problem with "I don't want to be forcing my users to be copying-n-pasting 25+ JARs"?
That's the easiest solution. And the best, because you will avoid problems in the future.
Now, when you see the inconveniences of Ant, you might want to compare it to Gradle. With Gradle you get tasks a bit similar to Ant and you don't need to provide any dependency jars. All dependencies for you will resolve Gradle. And like in Ant you still can create your tasks.
Some java application vendors use the following scenario to distribute their application which depend on other jars, it reminds of static linking. At the stage of building the jar all dependencies (being also jars) are unpacked. When building the final jar, they include both their freshly compiled classes and the classes extracted from dependencies.
Possible issues:
Applications cannot reuse the libraries as they are contained in the app. Usual static linking issue.
The licenses of the repacked libraries must be respected. Usually it will be ok to repack them, but sometimes additional care must be paid to their license files, which may happen to be inside their jars.
AFAIK it's not possible to have jars inside the jar or it will be impossible to specify a classpath for them. Hence the repacking procedure.
For example suppose I'm using the standard project structure and have
src/main/config/config.xml
To access this I presume
new File("src/main/config/config.xml");
would be incorrect
There is no "Maven Idiom" for accessing configuration files. Maven is a build platform, not an execution platform. So the conventions for accessing configuration files that apply are really just the conventions of the Java platform that you are using; e.g.
the plain J2SE way of doing it, or
the J2EE and/or webapp way of doing it, or
the J2ME way of doing it, or
...
Maven only comes into the picture because you (presumably) have resource files in your project / version control that need to be included in the JAR or WAR or whatever artifacts that you are building. To get this to work in Maven, you simply need to understand how Maven copies non-Java files into the artifacts.
In the simple (JAR) case, the default behavior is to copy anything in src/main/resources/ into the JAR, with the same relative name; e.g. src/main/resource/foo/bar.xml becomes /foo/bar.xml in the JAR file.
For WAR files, the default is to copy anything src/main/webapp to into the WAR file. So if you wanted a file to be accessible in the webapp as a classpath resource with the name /foo/bar.xml you would put it in src/main/webapp/WEB-INF/classes/foo/bar.xml. (I'm assuming that you know how webapp classpaths work ... or that this isn't your use-case.)
A config file is just a resource on your classpath like any other, so use:
URL resource = getClass().getResource("config.xml");
You'll need to do the usual Use as Source Folder on your src/main/config folder for this to work in Eclipse with m2e.
I think config files should be in src/main/resources by default.
Is there a best practice for where configuration files should be stored in a Java project. The file type is a Java properties file in this case, but I do use other file types in other projects.
Would the recommendation vary from stand alone application(.jar) to web app(.war)?
You'll find that many open-source projects follow the directory structure used by Maven. In this setup your application source code is kept in src/main/java, application resources, including properties files, in src/main/resources, and other config files in src/main/config. Files related to unit tests use a similar directory structure; src/test/java and src/test/resources.
Personally I tend to use this layout because of its widespread use. I also keep an "etc" directory beneath the project root to house files that aren't directly related to the application. For example, I keep configuration files for PMD and Checkstyle in etc.
In general a common practice is to have a resources directory for configuration files which is copied into the build artifact by the build process. Maven uses this in its default project structure. Within the resources directory, you might also have a META-INF directory and/or a WEB-INF directory in an application packaged as a war.
I use:
META-INF/ for jar files
WEB-INF/ for war files