I have created a maven war project, and want to call it from another module (war). When I searched found that name-sources.jar file is used as dependency to another module. I am adding dependency of name-sources but import is not working.
name-SNAPSHOT.war
name-SNAPSHOT-sources.jar
are the files generated on building of maven project.
Below is the dependency added into another module.
<dependency>
<groupId>uniqueid</groupId>
<artifactId>name</artifactId>
<version>8.9-SNAPSHOT</version>
<classifier>sources</classifier>
<scope>system</scope>
<systemPath>{Path}/name-8.9-SNAPSHOT-sources.jar</systemPath>
</dependency>
Any help is highly appreciated.
I understand this: you have 2 web projects A and B, both with maven packaging = war. In A, there are classes you want to use in B.
Forget about adding a *-sources.jar as dependency.
So, do this:
generate a new maven project C as a library with project packaging "jar"
Move the classes you want to share from A to C
Install this artifact C in your local maven repository (mvn install)
Then, add a dependency to C in the pom.xmls of A and B:
<dependency>
<groupId>your.group</groupId>
<artifactId>yourLibrary</artifactId>
<version>0.1.0-SNAPSHOT</version>
<scope>compile</compile> <!-- this is default, so this line is optional-->
</dependency>
Created *-classes.jar file. Use below code under pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
And added this new jar as dependency wherever want to use.
<dependency>
<groupId>com.honeywell.dras.sensibo</groupId>
<artifactId>sensibo-client</artifactId>
<version>{*}</version>
<classifier>classes</classifier>
</dependency>
So advantage with this is we can generate both war and jar files and make use of both based on need.
Related
I have an external .jar that cannot be imported from public repositories using pom.xml, it's sqljdbc41.jar.
I can run the project locally from my IDE, and everything will work. I referenced the library after downloading it like so:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc41.jar</systemPath>
</dependency>
When I run mvn clean package to create my .jar file and try to run the created .jar, a mistake will pop up, which mentions the SQL Server references are not valid. I then extracted my .jar file and true enough, everything that is referenced in the pom.xml file properly gets downloaded and added, however, my SQL Server does not.
I can, in a very hacky way* just manually add the sqljdbc41.jar to my /lib folder after it's been compiled as a .jar, and it'll work, however that seems highly unoptimal. What would be a better approach?
*Opening the .jar file with Winrar, going to the /lib folder, manually selecting my sqljdbc41.jar file, then make sure to select the No Compression option bottom left where Winrar gives you compression options, in case you find this by Google and no one answered.
you can set 'includeSystemScope' to true.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
You could install the sqljdbc41.jar in your local repository :
mvn install:install-file -Dfile=path/to/sqljdbc41.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc41 -Dversion=4.1 -Dpackaging=jar
And then declare the dependency as a standard dependency :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
</dependency>
If you use a remote artifact repository (nexus, archiva...) you also need to deploy the artifact on this repository. You can find more here : https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
Another way, you can put it into the resources folder, such as resources/lib/xxx.jar, then config the pom.xml like this:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/sqljdbc41.jar</systemPath>
</dependency>
In Spring Boot: I also faced similar issue and below code helped me.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
It works for me:
project {root folder}/libs/ojdbc-11.2.0.3.jar
pom.xml:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>11.2.0.3</version>
<scope>system</scope>
<systemPath>${basedir}/libs/ojdbc-11.2.0.3.jar</systemPath>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
In my case, the fault was providing a version number without "dot" in tag:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
This one works:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1.8</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
When Spring-Boot projects are used with maven or gradle plugins they packaged the applicaiton by default as executable jars.
These executable jars cannot be used as dependency in any another Spring-Boot project because the executable jar add classes in BOOT-INF/classes folder. This means that they cannot be found when the executable jar is used as a dependency because the dependency jar will also have the same class path structure as shown below.
If we want to use project-A as a maven dependency in project-B then we must have two artifacts. To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency.
To configure a classifier of exec in Maven, you can use the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
So the MAJIC WORD here is <classifier>exec</classifier> this will create a jar structure as below and then it could easily be conusmed by spring-boot project as maven dependency jar on class path.
The above plugin need to be add in project-A pom that is going to be used as dependency in project-B. Same is explained in spring documentation section 16.5. as well.
In order to work through the local repository, the target .jar file that we will work with must be in the s2 folder. Several methods can be used for this:
The file can be taken manually and put in the relevant place (not
preferred). The same process can be done by installing it via the
console.
Relevant Remote URL is written in the .pom file dependencies and
automatically places it in the s2 folder when Intellij is refreshed
(validate) in the IDE used.
The same process can be done by addressing the .pom file dependencies via the centeral repository.
Attention: ComponentScan should not be forgotten for the related jar work on SpringBot.
I have any modules in my spring-application on maven
First module build and I have war
Another Second module needs to import classes from a first.
Using this article this
I added this in first pom.xml
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
</configuration>
</plugin>
And after builing there is jar file in directory
Than I added dependency in second module
<dependency>
<groupId>ru.myProject</groupId>
<artifactId>MyProject-webapp</artifactId>
<version>${project.version}</version>
<classifier>classes</classifier>
</dependency>
and build all together
So, there are jar files in ear archive in lib folder, but when try to start - there is error message without logs.
Can you help me to fix it or get idea?
May be the root cause in that, second application already contained own spring-application context and initialization?
There are many "solutions" to this issue which I have tried to no avail, so please forgive me if this seems redundant.
I have a Java/Maven project being built with Intellij IDEA which has a dependency on the jar file built from this GitHub project: https://github.com/protegeproject/snap-sparql-query
Unfortunately, the jar is NOT in any external repository so must be built by me. I have the build working and manually copy the jar into the WEB-INF/lib folder of my parent project. Intellij then runs correctly, all dependent jars are found at execution even though the resulting war file does not contain the snap-sparql-query jar. I'm guessing it is getting cached somewhere.
If I build the project from the command line ($ mvn clean package) it builds but the above jar file still is NOT included in the resulting war file, even if it exists in the WEB-INF/lib folder of the parent before being packaged as a war file.
The ideal solution would be Maven commands in the parent that:
download the source for snap-sparql-query
compile the source into a jar
copy the jar to the parent WEB-INF/lib directory
all jars in the WEB-INF/lib directory get included in the war file
At the very least I'd be satisfied manually performing items 1-3 above, but have Maven perform #4.
Here is the Maven entry for snap-sparql-query:
<!-- SNAP SPARQL API -->
<!-- https://github.com/protegeproject/snap-sparql-query -->
<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>snap-sparql-query-api</artifactId>
<version>4.0.0-SNAPSHOT</version>
<!-- this library isn't found in the maven repository, must be externally compiled -->
<!-- and copied to the ...WEB-INF/lib directory so this pom can find it -->
<scope>system</scope>
<systemPath>${basedir}/WEB-INF/lib/snap-sparql-query-api-4.0.0-SNAPSHOT.jar</systemPath>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>de-derivo-sparqldlapi</artifactId>
<version>2.0.0</version>
<!--<version>3.0.0</version>-->
</dependency>
I've been warned using the systemPath is bad, so I would like to avoid that too, if possible.
Can I have Maven perform at least item 4 above, and/or ideally 1-4?
You asked multiple things and in order to answer all, I need more
details. I will update the answer once you provide those details. But for now, below is the answer for the Item 4 in your list.
By default Maven will not include System scoped jars in the packaged application. In order to include System scoped dependencies you need to use maven-dependency-plugin's copy- dependency goal.
Please note the <phase>prepare-package</phase>. Having phase prepare-package is very important to include the dependencies in the WAR file since this goal needs to be executed before the execution of maven-war-plugin.
<build>
<finalName>maven-sys-scope</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dep</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>system</includeScope>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I have 2 Maven web projects A and B. B contains some common parts and A depends on B.
In A's pom.xml I have:
<dependencies>
<dependency>
<groupId>com.mygroup</groupId>
<artifactId>B</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
</build>
I have 2 problems:
When making some changes in B, if I run a maven build on A I don't see the changes in the resulting exploded archive.
Trying to deploy A from Eclipse does not work - the contents of B are not included in the resulting war/exploded archive.
Thanks for your help.
Well, if you changes stuff in B, you have to re-install it into your local maven repo (mvn install) for other local projects that have it as dependency to receive the latest modifications.
When building a maven project it's best if you build it using Maven (like with commands such as mvn package) and not using some other building tool (such as Eclipse). If you wanna build it a la Maven but from the comfort of your Eclipse GUI, you can istall m2_eclipse plugin from :
http://m2eclipse.sonatype.org/installing-m2eclipse.html
which integrates Maven with Eclipse. Then, when you rigth click on your project in Eclipse, under the "Run..." options you'll have the one that allows your to Maven build it, redirecting all console output to the Eclipse console window.
And as a final note, in a setup such as the one aboce, ideally you'd create a parent Maven project (packaged as "pom") which has as child projects B and A (in that order). This way if you've modified stuff in both projects and you want everything to be build with the latest modifs, you can just do a maven install on the parent pom and Maven will take care of everything.
Amplifying #AndreiBodnarescu's point, you may not be seeing the changes you made to project-B when you build project-A because the changes aren't available in the Maven repository.
If project-B is being built on the same machine can you ensure that you used mvn install to install to your local repository? If project-B is being derived from a build on a different machine then use mvn deploy to deploy project-B to a common shared repository. In this case you may still not pick up project-B if you aren't using SNAPSHOTted versioning or you don't increment project-B's version number.
I see that B is of type war. What is the packaging of A? Is it an EAR? If so using the maven-war-plugin with project-A is not going to be of help.
It's my first couple of days learning Maven and I'm still struggling with the basics. I have an external .jar file (not available in the public repos) that I need to reference in my project and I'm trying to figure out what my best option is.
It's a small scale project without a central repository for libraries, so it has to be either a local repository (somehow added to source control, don't know if it's supposed to work that way?) or the .jar needs to be stored on disk outside of any formal repository.
1) What's my best option for adding the .jar file to my project's references with maven given that I want both the project and the library to be in source control?
2) I still can't seem to have Eclipse see the dependency. I manually added it to the section of the pom, and it shows up fine in the Dependencies list in m2eclipse. mvn compile and mvn package both succeed, but running the program results in:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
LibraryStuff cannot be resolved to a type
This is after editing the POM as:
<dependency>
<groupId>stuff</groupId>
<artifactId>library</artifactId>
<version>1.0</version>
<systemPath>${lib.location}/MyLibrary.jar</systemPath>
<scope>system</scope>
</dependency>
Should I be executing mvn install:install-file even thought I already have the pom.xml edited as above?
Thanks!
You can create an In Project Repository, so you don't have to run mvn install:install-file every time you work on a new computer
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/libs</url>
</repository>
<dependency>
<groupId>dropbox</groupId>
<artifactId>dropbox-sdk</artifactId>
<version>1.3.1</version>
</dependency>
/groupId/artifactId/version/artifactId-verion.jar
detail read this blog post
https://web.archive.org/web/20121026021311/charlie.cu.cc/2012/06/how-add-external-libraries-maven
I think you should use mvn install:install-file to populate your local repository with the library jars then you should change the scope from system to compile.
If you are starting with maven I suggest to use maven directly not IDE plugins as it adds an extra layer of complexity.
As for the error, do you put the required jars on your classpath? If you are using types from the library, you need to have access to it in the runtime as well. This has nothing to do with maven itself.
I don't understand why you want to put the library to source control - it is for sources code not binary jars.
This can be easily achieved by using the <scope> element nested inside <dependency> element.
For example:
<dependencies>
<dependency>
<groupId>ldapjdk</groupId>
<artifactId>ldapjdk</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
</dependency>
</dependencies>
Reference: http://www.tutorialspoint.com/maven/maven_external_dependencies.htm
The Maven manual says to do this:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
update We have since just installed our own Nexus server, much easier and cleaner.
At our company we had some jars that we some jars that were common but were not hosted in any maven repositories, nor did we want to have them in local storage.
We created a very simple mvn (public) repo on Github (but you can host it on any server or locally):
note that this is only ideal for managing a few rarely chaning jar files
Create repo on GitHub:
https://github.com/<user_name>/mvn-repo/
Add Repository in pom.xml
(Make note that the full path raw file will be a bit different than the repo name)
<repository>
<id>project-common</id>
<name>Project Common</name>
<url>https://github.com/<user_name>/mvn-repo/raw/master/</url>
</repository>
Add dependency to host (Github or private server)
a. All you need to know is that files are stored in the pattern mentioned by #glitch
/groupId/artifactId/version/artifactId-version.jar
b. On your host create the folders to match this pattern.
i.e if you have a jar file named service-sdk-0.0.1.jar, create the folder service-sdk/service-sdk/0.0.1/ and place the jar file service-sdk-0.0.1.jar into it.
c. Test it by trying to download the jar from a browser (in our case: https://github.com/<user_name>/mvn-repo/raw/master/service-sdk/service-sdk/0.0.1/service-sdk-0.0.1.jar
Add dependency to your pom.xml file:
<dependency>
<groupId>service-sdk</groupId>
<artifactId>service-sdk</artifactId>
<version>0.0.1</version>
</dependency>
Enjoy
Don't use systemPath. Contrary to what people have said here, you can put an external jar in a folder under your checked-out project directory and haven Maven find it like other dependencies. Here are two crucial steps:
Use "mvn install:install-file" with -DlocalRepositoryPath.
Configure a repository to point to that path in your POM.
It is fairly straightforward and you can find a step-by-step example here:
http://randomizedsort.blogspot.com/2011/10/configuring-maven-to-use-local-library.html
If you meet the same problem and you are using spring-boot v1.4+, you can do it in this way.
There is an includeSystemScope that you can use to add system-scope dependencies to the jar.
e.g.
I'm using oracle driver into my project.
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/extra-jars/ojdbc14-10.2.0.3.0.jar</systemPath>
</dependency>
then make includeSystemScope=true to include the jar into path /BOOT-INF/lib/**
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
and exclude from resource to avoid duplicated include, the jar is fat enought~
<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.jar</exclude>
</excludes>
</resource>
</resources>
</build>
Good luck!
Maven way to add non maven jars to maven project
Maven Project and non maven jars
Add the maven install plugins in your build section
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>${version.maven-install-plugin}</version>
<executions>
<execution>
<id>install-external-non-maven1-jar</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>jar1.group</groupId>
<artifactId>non-maven1</artifactId>
<version>${version.non-maven1}</version>
<file>${project.basedir}/libs/non-maven1.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>install-external-non-maven2-jar</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>jar2.group</groupId>
<artifactId>non-maven2</artifactId>
<version>${version.non-maven2}</version>
<file>${project.basedir}/libs/non-maven2.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>install-external-non-maven3-jar</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>jar3.group</groupId>
<artifactId>non-maven3</artifactId>
<version>${version.non-maven3}</version>
<file>${project.basedir}/libs/non-maven3.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
Add the dependency
<dependencies>
<dependency>
<groupId>jar1.group</groupId>
<artifactId>non-maven1</artifactId>
<version>${version.non-maven1}</version>
</dependency>
<dependency>
<groupId>jar2.group</groupId>
<artifactId>non-maven2</artifactId>
<version>${version.non-maven2}</version>
</dependency>
<dependency>
<groupId>jar3.group</groupId>
<artifactId>non-maven3</artifactId>
<version>${version.non-maven3}</version>
</dependency>
</dependencies>
References Note I am the owner of the blog
Change your systemPath.
<dependency>
<groupId>stuff</groupId>
<artifactId>library</artifactId>
<version>1.0</version>
<systemPath>${project.basedir}/MyLibrary.jar</systemPath>
<scope>system</scope>
</dependency>
The pom.xml is going to look at your local repository to try and find the dependency that matches your artifact.
Also you shouldn't really be using the system scope or systemPath attributes, these are normally reserved for things that are in the JDK and not the JRE
See this question for how to install maven artifacts.
Note that all of the example that use
<repository>...</respository>
require outer
<repositories>...</repositories>
enclosing tags. It's not clear from some of the examples.
The best solution here is to install a repository: Nexus or Artifactory. If gives you a place to put things like this, and further it speeds things up by caching your stuff from the outside.
If the thing you are dealing with is open source, you might also consider putting in into central.
See the guide.
With Eclipse Oxygen you can do the below things:
Place your libraries in WEB-INF/lib
Project -> Configure Build Path -> Add Library -> Web App Library
Maven will take them when installing the project.
If the external jar is created by a Maven project only then you can copy the entire project on your system and run a
mvn install
in the project directory. This will add the jar into .m2 directory which is local maven repository.
Now you can add the
<dependency>
<groupId>copy-from-the=maven-pom-of-existing-project</groupId>
<artifactId>copy-from-the=maven-pom-of-existing-project</artifactId>
<version>copy-from-the=maven-pom-of-existing-project</version>
</dependency>
This will ensure that you
mvn exec:java
works. If you use suggested here
<scope>system</scope>
Then you will have to add classes individually while using executing through command line.
You can add the external jars by the following command described here
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
The most efficient and cleanest way I have found to deal with this problem is by using Github Packages
Create a simple empty public/private repository on GitHub as per your requirement whether you want your external jar to be publicly hosted or not.
Run below maven command to deploy you external jar in above created github repository
mvn deploy:deploy-file \
-DgroupId= your-group-id \
-DartifactId= your-artifact-id \
-Dversion= 1.0.0 -Dpackaging= jar -Dfile= path-to-file \
-DrepositoryId= id-to-map-on-server-section-of-settings.xml \
-Durl=https://maven.pkg.github.com/github-username/github-reponame-created-in-above-step
Above command will deploy you external jar in GitHub repository mentioned in -Durl=.
You can refer this link on How to deploy dependencies as GitHub Packages GitHub
Package Deployment Tutorial
After that you can add the dependency using groupId,artifactId and version mentioned in above step in maven pom.xml and run mvn install
Maven will fetch the dependency of external jar from GitHub Packages registry and provide in your maven project.
For this to work you will also need to configure you maven's settings.xml to fetch from GitHub Package registry.