Let's say I have a maven project which has some maven modules inside.
My main module depends on the other modules, so when I compile the main module they should be compiled together.
The question is, how to add these modules as dependencies to the main module?
I know if I have a custom lib that I want to use with maven, let's say a utilities project, I have to compile the jar of the project, do a mvn install:install-file on it to install it on the local repository and then add it to the pom.xml.
Do I have to do this with all my modules and add the dependency to the pom.xml on my main module? Because if it should be done like this, there will be a lot of work to do when changing code on the other modules.
What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?
The question is, how to add these modules as dependencies to the main module?
The same way you add any other dependency to your maven project. By adding group id, artifact id and version to <dependency> element
Do I have to do this with all my modules and add the dependency to the pom.xml on my main module?
If your main module depends on some module A then only the pom of the main module should contain dependency declaration towards module A. You do that for all the dependencies of your module.
I don't know what you mean by "a lot of work when changing the code on other modules". Maven has nothing to do with code changes, it just builds the projects whatever they look like at the given moment...
What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?
Any project that you invoke mvn install on gets built and it's jar copied to local repository. That's all you need to do to get the jar into the repo. This will also put all the dependent jars, if available, into the local repo.
As for best practices for multi module projects:
If your parent project (the one that has modules inside) has <modules> section that lists the modules of your application, and modules are in subdirectories of your parent project, then you simply mvn install (or whatever you want to do) the parent project and that will cause all the modules to be built in order defined by declared dependencies between them. That means that if your main module has dependency on module A, then module A will be built before the main module. This way you can build and install all your modules with one command. On the other hand this approach makes more tight coupling between modules which is not desired in some cases, so it depends on your use case whether it is a good approach or not.
I have a java maven project that has another module as a dependency.
That dependency has "-sources.jar" in the repository (and I can download the sources with mvn dependency:sources)
I now need to do some little one-line changes in the module I am using as a dependency. The ideal way would be to somehow copy this dependency as another module, with all its current sub-dependencies. Then when I build the whole project, I want to build my newly copied dependency and use it instead of the current dependency. (I hope it's clear what I want to do.)
However, I don't know at all how to do that, and if it's even possible.
edit:
To be clearer.
I have my own project. I am also using let's say com.example.dependency from a repository. There are however small bugs in the com.example.dependency module, so I want to download the source to me and fix it locally and use it as a submodule.
edit2:
I will add that the dependency is not any public project on github, etc. It is a module in a private maven repository that only I have an access to. I cannot clone the source control project. I can only use maven, and its dependency tracking, and the "dependency:sources" thing.
what you may to to do is to checkout your dependency source as new project and do the changes. Then modify your project version and do the
mvn install
to install it in your local repository, then you this version in your project as one of the dependency, you will add this dependency in usual way you have done earlier, but change the version to new version you have installed.
simple scenario:
I have a maven project, containing some maven dependencies (activiti framework) and added the Widlfly 8.1.0 runtime as library in eclipse.
naturally now, if i clean and build with MAVEN, maven won't consider the runtime while compiling and complain that it cannot find eg. the #Webservlet Annotation, HttpRequest classes etc.
so in order to build my Project, i have to run any maven goal and see it fail, just to have maven download all dependencies, then build the project with eclipses' build process which then uses all downloaded maven dependencies AND the wildfly 8.1.0 runtime, succeeding in building the project.
THEN only can I run maven install/deploy to create the .war, which works, because maven finds a compiled target folder, created by eclipse.
How can i, without instlalling all runtime jars to my local repository or adding the wildfly installation as antoher local repo, tell maven or the m2e plugin to include manually added libraries to mavens compile step?
what you probably want is a "provided"*-scope dependency on org.wildfly:wildfly-spec-api:8.1.0 (which is a pom artifact containing all the apis/specifications provided to you by wildfly).
assumming you intend to deploy your app inside wildfly (as opposed to embedding wildfly in your own main() somehow...) you dont need a dependency on the wildfly container.
* - note that since wildfly-apec-api is a pom artifact (and not a jar) you need to use the import scope and not provided. see this article for a complete guide. the gist is you put an import scope dependency on the pom in dependency management, and then you can put a provided-scope dependency on any specific member api/spec that you use (say ejb3, jsf, bean validation or jpa) and the versions will be taken from the spec-api pom.
I have a maven project which own the following structure:
api(with packaging pom)
pom.xml
api-common(with packaging jar)
pom.xml
api-webapp(with packaging war)
pom.xml
The api is the parent of api-common and api-webapp.
api-webapp have a dependency of api-common.
Now when I run mvn jetty:run inside the api/api-webapp,jetty can not auto load the change of the module api-common.
Any idea?
BTW,I use maven3
Since api-webapp depends on api-common and they are separate projects, api-webapp will look for it's dependencies in the maven repository: your .m2 directory, then the remote repo. You'll need to compile and install your changes in api-common first before api-webapp can see them.
api-webapp has no knowledge of the location of the source of api-common so it can not detect changes. The fact that they share a common parent does not give them the ability to see each other.
That's the general maven answer, but the jetty plugin does provide a workaround to this issue by letting you provide additional classpath for the container. See this defect
What is an artifact and why does Maven need it?
An artifact is a file, usually a JAR, that gets deployed to a Maven repository.
A Maven build produces one or more artifacts, such as a compiled JAR and a "sources" JAR.
Each artifact has a group ID (usually a reversed domain name, like com.example.foo), an artifact ID (just a name), and a version string. The three together uniquely identify the artifact.
A project's dependencies are specified as artifacts.
In general software terms, an "artifact" is something produced by the software development process, whether it be software related documentation or an executable file.
In Maven terminology, the artifact is the resulting output of the maven build, generally a jar or war or other executable file. Artifacts in maven are identified by a coordinate system of groupId, artifactId, and version. Maven uses the groupId, artifactId, and version to identify dependencies (usually other jar files) needed to build and run your code.
I know this is an ancient thread but I wanted to add a few nuances.
There are Maven artifacts, repository manager artifacts and then there are Maven Artifacts.
A Maven artifact is just as other commenters/responders say: it is a thing that is spat out by building a Maven project. That could be a .jar file, or a .war file, or a .zip file, or a .dll, or what have you.
A repository manager artifact is a thing that is, well, managed by a repository manager. A repository manager is basically a highly performant naming service for software executables and libraries. A repository manager doesn't care where its artifacts come from (maybe they came from a Maven build, or a local file, or an Ant build, or a by-hand compilation...).
A Maven Artifact is a Java class that represents the kind of "name" that gets dereferenced by a repository manager into a repository manager artifact. When used in this sense, an Artifact is just a glorified name made up of such parts as groupId, artifactId, version, scope, classifier and so on.
To put it all together:
Your Maven project probably depends on several Artifacts by way of its <dependency> elements.
Maven interacts with a repository manager to resolve those Artifacts into files by instructing the repository manager to send it some repository manager artifacts that correspond to the internal Artifacts.
Finally, after resolution, Maven builds your project and produces a Maven artifact. You may choose to "turn this into" a repository manager artifact by, in turn, using whatever tool you like, sending it to the repository manager with enough coordinating information that other people can find it when they ask the repository manager for it.
Hope that helps.
Maven organizes its build in projects.
An artifact in maven is a resource generated by a maven project. Each maven project can have exactly one artifact like a jar, war, ear, etc.
The project's configuration file "pom.xml" describes how the artifact is build, how unit tests are run, etc.
Commonly a software project build with maven consists of many maven-projects that build artifacts (e.g. jars) that constitute the product.
E.g.
Root-Project // produces no artifact, simply triggers the build of the other projects
App-Project // The application, that uses the libraries
Lib1-Project // A project that creates a library (jar)
Lib2-Project // Another library
Doc-Project // A project that generates the user documentation from some resources
Maven artifacts are not limited to java resources. You can generate whatever resource you need. E.g. documentation, project-site, zip-archives, native-libraries, etc.
Each maven project has a unique identifier consiting of [groupId, artifactId, version]. When a maven project requires resources of another project a dependency is configured in it's pom.xml using the above-mentioned identifier. Maven then automatically resolves the dependencies when a build is triggered. The artifacts of the required projects are then loaded either from the local repository, which is a simple directory in your user's home, or from other (remote) repositories specified in you pom.xml.
Q. What is Artifact in maven?
ANS: ARTIFACT is a JAR,(WAR or EAR), but it could be also something else. Each artifact has,
a group ID (like com.your.package),
an artifact ID (just a name), and
a version string. The three together uniquely identify the artifact.
Q.Why does Maven need them?
Ans: Maven is used to make them available for our applications.
An artifact is a JAR or something that you store in a repository. Maven gets them out and builds your code.
To maven, the build process is arranged as a set of artifacts. Artifacts include:
The plugins that make up Maven itself.
Dependencies that your code depends on.
Anything that your build produces that can, in turn be consumed by something else.
Artifacts live in repositories.
usually we talking Maven Terminology about Group Id , Artifact Id and Snapshot Version
Group Id:identity of the group of the project
Artifact Id:identity of the project
Snapshot version:the version used by the project.
Artifact is nothing but some resulting file like Jar, War, Ear....
simply says Artifacts are nothing but packages.
Usually, when you create a Java project you want to use functionalities made in another Java projects.
For example, if your project wants to send one email you dont need to create all the necessary code for doing that. You can bring a java library that does the most part of the work.
Maven is a building tool that will help you in several tasks. One of those tasks is to bring these external dependencies or artifacts to your project in an automatic way ( only with some configuration in a XML file ).
Of course Maven has more details but, for your question this is enough.
And, of course too, Maven can build your project as an artifact (usually a jar file ) that can be used or imported in other projects.
This website has several articles talking about Maven :
https://connected2know.com/programming/what-is-maven/
https://connected2know.com/programming/maven-configuration/
An artifact is an element that a project can either use or produce. In Maven terminology, an artifact is an output generated after a Maven project build. It can be, for example, a jar, war, or any other executable file.
Also, Maven artifacts include five key elements, groupId, artifactId, version, packaging, and classifier. Those are the elements we use to identify the artifact and are known as Maven coordinates.
read it from here