Maven pom.xml local dependency not Resolved - java

Using eclipse maven and java 1.7 I have designed two backends named webservice.staff.backend and webservice.webuser.backend .
Now There is another webservice which is written by some of our Senior Team Member, which is a another module for Project shared with me by git.
In this New Project in pom.xml there are two local project dependency have been added as Follows.
<dependency>
<groupId>webservice.staff.backend</groupId>
<artifactId>webservice.staff.backend</artifactId>
<version>1.0.0</version>
<classifier>classes</classifier>
<type>jar</type> //used jar but webservices packaging as a war
</dependency>
<dependency>
<groupId>webservice.webuser.backend</groupId>
<artifactId>webservice.webuser.backend</artifactId>
<version>1.0.0</version>
<classifier>classes</classifier>
<type>jar</type> // used jar but webservices packaging as a war
</dependency>
but these dependency is not resolved when I am just trying to perform Maven clean install. What I Found in my earlier webservice Projects are packaging as a WAR but here used as jar
Here is earlier Project packaging type in pom.xml.
<groupId>webservice.staff.backend</groupId>
<artifactId>webservice.staff.backend</artifactId>
<packaging>war</packaging> //packaged as war but used as jar??
<groupId>webservice.webuser.backend</groupId>
<artifactId>webservice.webuser.backend</artifactId>
<packaging>war</packaging> //packaged as war but used as jar in
/*I Have also Changed packaging as a jar and then clean Install
and try to build the New Project but error still the same. */
Error StackTrace
[ERROR] Failed to execute goal on project webservice.credit.backend: Could not resolve dependencies for project webservice.credit.backend:webservice.credit.backend:war:2.0.0: The following artifacts could not be resolved: webservice.staff.webservice.staff.backend:jar:classes:1.0.0, webservice.webuser.backend:webservice.webuser.backend:jar:classes:1.0.0: Failure to find webservice.staff.backend:webservice.staff.backend:jar:classes:1.0.0 in http://download.java.net/maven/2/ was cached in the local repository, resolution will not be reattempted until the update interval of Java.Net has elapsed or updates are forced -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
Note :- I have tried so many ways and also so many answers on stackOverflowin this regard but no one gives me correct solution to that.
Please Help anybody to Resolve this.. Thanks.

Remove <classifier>classes</classifier>
<dependency>
<groupId>webservice.staff.backend</groupId>
<artifactId>webservice.staff.backend</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>webservice.webuser.backend</groupId>
<artifactId>webservice.webuser.backend</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
pom will be same for both projects except packaging will be jar :
<groupId>webservice.staff.backend</groupId>
<artifactId>webservice.staff.backend</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<groupId>webservice.webuser.backend</groupId>
<artifactId>webservice.webuser.backend</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
Make sure your settings.xml is like below :
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.home}/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
------
</settings>
I have uploaded sample here.

Finally !!!
Got the solution for this.
what i need to do during clean & install add extra param with mvn to export the class files. parameter ==> -Dattach.and.archive.classes=true to the mvn command.
mvn clean install -Dattach.and.archive.classes=true //added one extra param
// -Dattach.and.archive.classes=true solved my dependency Problem.
Note :- If you Defined in pom.xml Project packaging as war and want a jar as dependency in Your local maven Repository. Just put this extra Option with -Dattach.and.archive.classes=true with mvn.

Related

why one of my dependency's maven coordinates in pom file changed follow the new project's maven coordinates

I have created a maven archetype from a project, in this project's pom file, there's an dependency demo-admin
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-admin</artifactId>
<version>${demo-admin.version}</version>
</dependency>
this dependency is installed in my local repository.
But when I checked the generated archetype files, the pom.xml file in the archetype-resources folder, the dependency looks as below
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-admin</artifactId>
<version>${version}</version>
</dependency>
so every times I create a new project from the archetype, the maven coordinates of this denpendency will change.
Anybody has the same issue?
EDIT:
This issue could be reproduced as below steps:
create a project which GAV as below
<groupId>com.demo</groupId>
<artifactId>abc-admin</artifactId>
<version>1.0.0</version>
create a project which GAV as below
<groupId>com.demo</groupId>
<artifactId>abc</artifactId>
<version>1.0.0</version>
the second project contains a dependency
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo-admin</artifactId>
<version>1.0.0</version>
</dependency>
create an archetype from the second project using the command below
a. cd abc
b. mvn clean
c. mvn archetype:create-from-project
go the "abc/target/generated-sources/archetype/src/main/resources/archetype-resources" folder, you can see a pom file, in the pom file, the dependency looks as below
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${rootArtifactId}-admin</artifactId>
<version>${version}</version>
</dependency>
so, if I create a new project based on the abc archetype, when I input different GAV, the dependency will also changed based on the input values
I guess this is the automatic replacement of your of the groupId and artifactId of your demo project by placeholders.
Either give you demo project some name that never appears anywhere else or edit the resulting archetype by replacing the placeholders with the correct values.

Maven project is missing package when compiling from terminal

I'm currently trying to compile my first maven project from the command line but whenever I run this command:
C:\Users\zacha\git\INF2050\tp1\PELZ07039904\src\main\java>javac analyse.java
I get this back
analyse.java:7: error: package org.apache.commons.io does not exist
But the project runs perfectly fine when I run it directly from InteliJ
Here is my pom.xml file:
<?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>org.example</groupId>
<artifactId>PELZ07039904</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
I checked and all my jar are in my .m2 file so I'm not sure why I'm getting this error.
javac doesn’t have any idea about maven and it’s dependencies. All it can do is just compile a class. Thats it.
Instead you need to use maven specific command.
Maven provides a command line tool.
To build a Maven project via the command line, run the mvn command from the command line. The command should be executed in the directory which contains the relevant pom file. You need to provide the mvn command with the life cycle phase or goal to execute.
The Maven tooling reads the pom file and resolves the dependencies of the project. Maven validates if required components are available in a local repository. The local repository is found in the .m2/repository folder of the users home directory.
For example the mvn clean install command triggers the jar packaging. This includes compiling the sources, executing the tests and packaging the compiled files in a JAR file. As last step the install phase installs the resulting artifact into the local repository, so it can be used as dependencies by other Maven builds.
Check full guide here

Configure local jar in pom.xml

In my maven project I need to use some local jar libraries as a dependency.
I'm trying to use this method to configure them in pom.xml:
<repositories>
<repository>
<id>projectName.local</id>
<url>file://${project.basedir}/libs</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.myproject</groupId>
<artifactId>ProjectName</artifactId>
<version>1.0</version>
</dependency>
<!-- other dependencies... -->
</dependencies>
I defined a local repository that points to a libs folder into the project root. Then I placed the jars in this folder:
/libs/org/myproject/ProjectName/1.0/ProjectName-1.0.jar
But, building the project I'm getting this warning:
The POM for org.myproject:ProjectName:jar:1.0 is missing, no dependency information available
And this build failure result:
Failed to execute goal on project my_project: Could not resolve dependencies for project *** : The following artifacts could not be resolved: org.myproject:ProjectName:jar:1.0: Failure to find org.myproject:ProjectName:jar:1.0 in file://C:\Users\David\Documents\NetBeansProjects\my_project/libs was cached in the local repository, resolution will not be reattempted until the update interval of projectName.local has elapsed or updates are forced -> [Help 1]
What am I missing?
I think just copying the jar file will not solve your problem. You will need to install the dependency using Maven itself in that repository using a command like this one:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=your.groupId \
-DartifactId=your-artifactId \
-Dversion=version \
-Dpackaging=jar \
-DlocalRepositoryPath=path-to-specific-local-repo
path-to-specific-local-repo should be the path to your local repository (file://${project.basedir}/libs).
Define the pom.xml like dependency.
<dependency>
<groupId>org.myproject</groupId>
<artifactId>ProjectName</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jarfile</systemPath>
</dependency>
Put the all the files in the folder
You can add your local jar as:
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>
Local works for you by doing above.If you want to pack this local jar in your fat jar then include following line in maven plugin configuration:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>

Caching Maven parent in offline build

TL/DR: How can I cache the pom.xml file's <parent> so my build can be run in offline mode?
I'm using Docker to build a maven project. My goal is to add two steps to the build: one to download all of the dependencies, and another to build the project. Here's what my Dockerfile looks like so far:
FROM maven:3.3-jdk-8
# Download the project dependencies (so they can be cached by Docker)
ADD pom.xml /runtime/
WORKDIR /runtime
RUN mvn dependency:go-offline
RUN mvn dependency:resolve-plugins
# Mount the local repository
ADD . /runtime
# Build the service
RUN mvn clean package -o -DskipTests
This seems to work fine for the plugins. I checked the /root/.m2/repository and everything seems to be in order.
Edit: When double checking for the /root/.m2/repository directory, it's no longer there. For some reason, Maven isn't saving any of the dependencies to this location.
Edit 2: After building the Docker image, there's no /root/.m2/repository directory. However, if I run mvn dependency:go-offline from within a shell inside the Docker container, the directory is created without a problem.
When I attempt build my application, I get the following error:
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.example:service:1.2: Cannot access central (http://jcenter.bintray.com) in offline mode and the artifact org.springframework.boot:spring-boot-starter-parent:pom:1.4.0.M3 has not been downloaded from it before. and 'parent.relativePath' points at wrong local POM # line 14, column 13
#
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project com.sample:service:1.2 (/runtime/pom.xml) has 1 error
[ERROR] Non-resolvable parent POM for com.oe:graph-service:1.2: Cannot access central (http://jcenter.bintray.com) in offline mode and the artifact org.springframework.boot:spring-boot-starter-parent:pom:1.4.0.M3 has not been downloaded from it before. and 'parent.relativePath' points at wrong local POM # line 14, column 13 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
The problem seems to be that mvn dependency:go-offline isn't resolving the parent. When I run the build in offline mode, it breaks.
Here are the relevant portions of my pom.xml file:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.M3</version>
</parent>
...
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<id>repository.springsource.snapshot</id>
<name>SpringSource Snapshot Repository</name>
<url>http://repo.springsource.org/snapshot</url>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.scala</groupId>
<artifactId>spring-scala_2.11</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- disabling Spring cloud AWS until proper testing harnesses can be set up -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-aws-autoconfigure</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
...
</dependencies>
...
</project>
If you customize maven's settings.xml file you can save your repository files on image.
Create a custom version of settings.xml, with the localRepository setting modified, like this:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository -->
<localRepository>/usr/share/maven/repo</localRepository>
...
Then override the default configuration when building your image:
FROM maven:3-jdk-8
COPY settings.xml /usr/share/maven/conf/settings.xml
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ADD . /usr/src/app
RUN mvn dependency:go-offline
RUN mvn clean package
Now your repository is stored in /usr/share/maven/repo.
Using onbuild
You can also create a base image using ONBUILD, this will allow to have custom configured images for every maven project.
Like this:
FROM maven:3-jdk-8
COPY settings.xml /usr/share/maven/conf/settings.xml
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ONBUILD ADD . /usr/src/app
ONBUILD RUN mvn dependency:go-offline
ONBUILD RUN mvn clean package
Then build the image:
docker build -t mvn_bldr .
This will create a template for other maven images. Then you can create your custom downstream image with:
FROM mvn_bldr
If you want to customize your image further you can add more instructions, every instruction of the template will be triggered after the FROM mvn_bldr command, as in the docs:
The trigger will be executed in the context of the downstream build,
as if it had been inserted immediately after the FROM instruction in
the downstream Dockerfile.
It may seem like using RUN with Docker is like executing commands in a shell script, but it's not. Every instance of RUN gets applied to a new container that results from the changes created by the previous command. So each command is executing inside of a new container context.
Dockerifles can contain a VOLUME reference, which mounts an external directory inside the Docker container. If there were any files inside the volume, those files are wiped out. If you don't explicitly specify a volume, Docker is happy to instead create an empty folder.
While my Dockerfile doesn't contain an explicit reference to a VOLUME, its parent does. So, even though my mvn dependency:go-offline command was running, those files were being wiped out in the next step by the VOLUME specified in the docker-maven Dockerfile.
In the end, I couldn't find a good way to make the maven Docker image work, so I switched to the openjdk image and installed maven via apt-get instead.

Maven Multi-module: Submodule as dependencies

I have a multi-module project in maven and I created a main sub-module(which means that this sub-module has the main class). I add the other sub-modules into the main the sub-module(which is Submodule-0) as dependencies:
<dependencies>
<dependency>
<groupId>ProjectGroupId</groupId>
<artifactId>Submodule-1</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ProjectGroupId</groupId>
<artifactId>Submodule-2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ProjectGroupId</groupId>
<artifactId>Submodule-3</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
I have Artifactory in my LAN and my maven settings.xml is configured to connect into the local Artifactory. In the PC where I installed the Artifactory this setup works fine but when I ran the setup in another computer(my laptop) it can't find the sub-modules libraries but I can see it in the Dependencies section of the project using Netbeans.
The Error Generated is:
[ERROR] Failed to execute goal on project Submodule-0: Could not resolve dependencies for project ProjectGroupId:Submodule-0:jar:0.0.1: Failed to collect dependencies at ProjectGroupId:Submodule-1:jar:0.0.1: Failed to read artifact descriptor for ProjectGroupId:Submodule-1:jar:0.0.1: Failure to find ProjectGroupId:MultiModule:pom:0.0.1 in http://192.168.10.136:8081/artifactory/libs-release was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
I hope you guys can help and if this is a duplicate question hope you guys can lead me in the right path.
Try to run a maven build with the goal "deploy" on the machine with Artifactory installed. With that the artifacts are uploaded to the Artifactory's repository and will be available for remote access.
Once you have a stable version you may want to take a look into the maven-release-stuff http://maven.apache.org/maven-release/maven-release-plugin/index.html
EDIT:
Did you copy the settings.xml to the other machine?

Categories