Maven build - dependancy on core classes - java

I have two projects I am working on which share some common code - I am putting this common code in to a new project called Core.
Both my projects use maven to build, and my core classes will also use maven. In Eclipse how do I configure maven to do a maven build of the core classes and then use these in the build for my two other applications?
Is there some prebuilt rule I need to specify - for example build this project, however, go build core first and use the output of that for this.
Hope that makes some sort of sense.

You can add a dependency to Core artifact in your project.
If you use M2E Eclipse plug-in, workspace artifacts are quite easy to reference in the Maven editor.
The only condition for this kind of dependency to work is that dependent artifact must be retrievable from a maven repository (eventually putting it to the local maven repository through install goal).

You could add a parent pom, with modules
<project>
<groupId>com.mypackage</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<packaging>pom</packaging>
...
<modules>
<module>Core</module>
<module>MyModuleA</module>
<module>MyModuleB</module>
</modules>
...
</project>
And then just add your dependency in MyModuleA and MyModuleB like a normal dependency
<dependency>
<groupId>com.mypackage</groupId>
<artifactId>Core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

Related

Maven and IntelliJ modular project

I have two dependencies one of them is built on top of the other.
The first project is with POM
<parent>
<groupId>com.company</groupId>
<artifactId>bom</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<groupId>com.company.site</groupId>
<artifactId>site</artifactId>
<packaging>war</packaging>
<dependency>
<groupId>com.company.platform</groupId>
<artifactId>platform</artifactId>
</dependency>
This project is built on top of another project (dependence)
which have the following POM
<parent>
<groupId>com.company</groupId>
<artifactId>bom</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>platform</artifactId>
<packaging>pom</packaging>
How can I import these two projects in Intellij so when i make changes in sources in the platform project, these projects two be available in site project.
I have access to both depositories and their sources.
Best regards.
If you must have 2 projects (instead of a single multi-module project) it's really quite simple - open two intellij windows, one for site and one for platform.
Next, you need to define group and version for platform.
<groupId>com.company.platform</groupId>
<artifactId>platform</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
Now update the pom for site with the 1.0.0-SNAPSHOT version for platform dependency.
Your workflow is like this:
make change in platform project, use Maveh Projects window to build (clean install)
switch to site project, which depends on platform-1.0.0-SNAPSHOT, use Maven Projects window to Reimport all Maven Projects
You should then see the code updates from platform in site project.
When you want to go in production you need to go from -SNAPSHOT to release (non-snapshot) dependencies.

Resolve maven dependencies from child pom

I'm quite new to maven and I have a maven multi module project with the parent pom as
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.cit</groupId>
<artifactId>cit</artifactId>
<version>LATEST-SNAPSHOT</version>
<name>Integration Test Framework</name>
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>core</module>
<module>login</module>
</modules>
</project>
I have put all the relevant external dependencies to the child poms of common, core and login. I then converted the project to an eclipse project (mvn eclipse:eclipse) and after that eclipse is unable to resolve the dependencies in the child pom though the respective jars are present in M2_HOME.
I then added all the dependencies to the parent pom(Whatever dependency was there in the child poms) from the child poms and then eclipse was able to resolve that.
I'm confused of this behavior. Since I've already added the external dependencies to the child poms why should I add again that to the parent pom?
Anybody could you please explain this or am I doing something wrong here to fix the problem.
In the parent pom you have the option to add two tags :
<dependencies></dependencies>
and
<dependencyManagement></dependencyManagement>
In the <dependencies> tag you have to place all the dependencies that you want that all you projects includes, for example JUnit dependency or Log4j.
In the <dependencyManagement> tag you should add all the dependencies that your project needs, It does not means that they are going to be included in all your projects. It only means that your child projects can include them or not. It is going to help you to manage the versions.
Maybe you have problems in the dependency bewteen your child projects for this reason when you add all the dependencies to your parent project It works.

How to reference one maven project from another maven project?

I have an in-development library project (module by IntelliJ definition) that i want to reference from other projects. How would i go about to reference that project in other projects ?
you can use whether Dependency or module tags in pom.xml of your project.
Depends on what you trying to do.
<dependency>
<groupId>com.mysubpro</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Or
<modules>
<module>myproject</module>
</modules>
You have to use mvn install goal. This will install your library to your local repository. Link to Maven Install Plugin. Once it is done you can have a dependency to it in your other projects.

Mulit-module application maven

I've got multi module maven project, where main project depend on sub-module. Every dependency of sub-module is define by version like this: ${pom.version}. I use maven release plug-in. If I try to prepare release, I've got an error about missing version of sub-module.
Example:
main pom is on version 1.0, I try to release it. Maven build every sub-module to version 1.1, then try to build parent, and then crash. Because it can't find sub-module-1.1.
I don't know how to tell maven to build, and immediate install to local-repo every sub-module witch it build. I use maven2.
My pom:
<modelVersion>4.0.0</modelVersion>
<groupId>com.voncuver</groupId>
<artifactId>voncuver</artifactId>
<packaging>pom</packaging>
<version>1.1-SNAPSHOT</version>
<name>multimodule</name>
<modules>
<module>mod1</module>
<module>mod2</module>
</modules>
(...)
<dependencyManagement>
<dependencies>
<dependency>
<artifactId>mod1</artifactId>
<groupId>com.voncuver</groupId>
<version>${pom.version}</version>
</dependency>
<dependency>
<artifactId>mod2</artifactId>
<groupId>com.voncuver</groupId>
<version>${pom.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
(...)
You should probably post a bit more of your project structure, but typically a multimodule project looks like this:
project
mod1
mod2
mod3
pom.xml
The main pom.xml would have "pom" packaging type, and have a section in it to build everything else:
<packaging>pom</packaging>
<modules>
<module>mod1</module>
<module>mod2</module>
<module>mod3</module>
</modules>
Then, the surest way to make sure things build properly is to execute:
mvn clean install
Without the "install", it's highly possible that things might not be found in the maven reactor, especially depending on what version of maven you are using (and a few other factors).

Can't find class from maven dependency project

[CLOSED - I had to move classes from test/java to main/java and update the maven repository via the IDE "maven options"]
I'm new to maven and inexperienced with java development. I'm using IntelliJ Idea as IDE. I'm using Maven 3.0.4.
I created a "project A" and a "project B", each with some classes. Now when I try to create a dependency in project A to a class in project B I can't seem to find any classes that are part of project B. When I check the maven repository I can see that a .jar file is created based on project B.
To clarify: when adding a dependency to project B I do find an artifact called "project B" but I cannot find any classes that are part of project B.
It doesn't seem like I can access and use any of the classes that are part of project B inside project A which would make this installation worthless.
--
Please tell me what information I should include for you to help me solve this problem.
[EDIT] Here is the project A's pom with a dependency on project B. However I either don't understand how to use it in my project or it doesn't work. While I can use IntelliJ's functionality to find and add the artifact, intelliJ can't seem to find any classes that are part of project B (while it does find classes that are part of other pre-defined packages in the maven repository):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>planet</groupId>
<artifactId>planet</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>toolbox</groupId>
<artifactId>toolbox</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
I had to move my classes from test/java to main/java. Silly, but it took me a whole day to realise this. This only worked after updating the repository in "maven options" via the IDE.
You can do the following:
<dependency>
<groupId>toolbox</groupId>
<artifactId>toolbox</artifactId>
<version>1.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Works for me :)

Categories

Resources