How can I add SlingSafeMethodsServlet to this AEM sample project through maven? - java

I'm using the Eclipse AEM plugin and am importing a sample project. Unfortunately, this sample project does not contain certain dependencies that the accompanying tutorial asks you to use, so I'd like to add those dependencies.
I'd like to end up being able to access this class:
https://sling.apache.org/apidocs/sling5/org/apache/sling/api/servlets/SlingSafeMethodsServlet.html
In my project's pom.xml, I have this included:
<!-- https://mvnrepository.com/artifact/org.apache.sling/org.apache.sling.api -->
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
<version>2.14.2</version>
</dependency
After doing mvn clean install, I can see that the .jar is saved to the /target directory. However, if I go to add this as a library in Properties > Java Build Path > Add JARs, I don't see it.
I'm able to add it by going to "Add External JARs", but this seems to bypass maven.
What am I missing?

Ideally, you don't need these downloaded in your target folder. Most of the sling, osgi, aem dependencies are available in AEM instance thus no need to package.
You could simple configure your project to pull all dependencies from .m2 folder. All these dependencies should be set to <scope>provided</scope>. Refer to sample pom.xml here, this one uses Uber.jar so doesn't specify the sling dependencies explicitly.
Also make sure that your project has maven local repo configured to .m2/repositories
UPDATE

Related

Install third party jar in local maven before maven resolves its dependency?

I am working on a multi-module maven project and have third party jar which isn't available in central or public repository, I also even don't want to place on public repo. I am providing following plugin directive in my parent pom to install jar in my local maven repository before its dependency is resolved in child project.
Now I provide dependency in child project as;
But I build the project, it successfully adds dependency in local maven repository (places third party jar in .m2 folder) but the at the same time it gives following error. Looks like, it searches this jar file in child projects libs folder as well, while I have already placed it on root (in myproject.parent/libs).
Failed to execute goal org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file (install-
external-non-maven1-jar) on project myProject.core: The specified file 'C:\Users\myuser\git\MyProjectRepository\myproject.parent\myproject.core\libs\be-ixf-java-sdk-jar-with-dependencies.jar' not exists.
I already know scope and systemPath option but I don't want to use it for some reason.
Kindly help in identifying what I am missing ?
The best approach that you could have if your project have a centralized maven repo like nexus setup is to have your third party library also added to the repo. Now , you are having the bin file added to your project and it's not preferable.
If you already have the required jar under your project code in like : libs\*, then why can't you refer the dependency directly in your pom.xml instead of having to install it in your local maven repo and then use it .
<dependency>
<groupId>anything</groupId>
<artifactId>anything</artifactId>
<version>anything</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jar_name.jar</systemPath>
</dependency>
providing the location of the dependency in your project directory should resolve it during build itself.Look at Maven System dependencies.
Since you do not want to change your current setup . Please bear in mind the following about maven pom structure :
Project Aggregation is similar to Project Inheritance. But instead of
specifying the parent POM from the module, it specifies the modules
from the parent POM. By doing so, the parent project now knows its
modules, and if a Maven command is invoked against the parent project,
that Maven command will then be executed to the parent's modules as
well
So the maven-install-plugin execution that you added in main pom.xml might be getting triggered for all your modules because of this reason. You might want to move that to the module that you actually need this lib to be included in.Read maven doc.
Related : force Maven to copy dependencies into target/lib

Build Maven project with local dependencies

I downloaded old project, which based on Eclipse, Ant or something else, I don't know this build system (I use Maven\Gradle). Project sources was converted to Maven manually, but project had more jar libraries. They were imported in project with help of IDEA (Project Structure -> Modules -> Dependencies), but libraries defined only in .iml file.
When I tried to build project in .jar with help of Maven — Maven show errors, that it cannot find classes from libraries (but in sources all good). I found in Internet example, like in this code sample:
<dependency>
<groupId>example</groupId>
<artifactId>example</artifactId>
<version>examle</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/example.jar</systemPath>
</dependency>
But this don't help. Libraries are old too, and they created by old build tools, which don't write artifact id, group id and etc., so I don't know this information.
How I can write dependencies on local .jar libraries in pom.xml if I don't know information (group, artifact ids) about they?
UPD_0:
When I try to set only system path to library, I take this:
All you need is set correct path in the
<systemPath>${project.basedir}/libs/example.jar</systemPath>
group, artifact ids and version are user defined information. So you can define it as you wish.
Try to install this jar in ur local repository from command line / terminal like this. Then add the dependency with the package and version given by you in the command without scope as system
mvn install:install-file -Dfile=<path>/example.jar
-DgroupId=com.something
-DartifactId=example
-Dversion=<give some version>
-Dpackaging=jar
-DgeneratePom=true

Install library jar in pom maven

I have Java Maven Project with a folder lib with all jars that must be included in my project.
I don't know how I have to modify the POM to add all libraries. I want that Maven uses this libraries and I can use all in the project.
Lets be frank here: you are basically asking how to use Maven but then not use it at the same time. Maven is built around the fact that dependencies are managed from dependable repositories and then you come along and want to bypass that entire system by having local jars anyway as you would have in a project not managed through Maven - like one that is built with ANT.
The true clean solution to "not getting jars from a Maven repository" is to still get them from a repository - your own. Setup a local repository and put your third party dependencies there, then configure your Maven build to know about that local repository. If they are actually dependencies that exist in Maven central then you can setup your local repository to proxy them rather than manually installing them yourself.
http://maven.apache.org/repository-management.html
If you are using the release management features of Maven (or something like Hudson) then you should actually already have such a thing to stick your generated release artifacts into.
You have to know which libraries are you using. Imagine that you use in your project the joda-time library.
So you have to search your libraries (in this case joda-time) in maven repositories and add to your pom.xml like the following:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
Here the Maven repository --> Maven Repo

add external jar to our dependency

There is a jar file lets say "abc.jar" which maven dependency does not exist(ie created a jar by using java command of own classes). I want to add this jar as maven dependency so that at build time it will automatically copy that jar in lib folder as like other maven dependency. how i will do. please help .
Add it as a dependency with a system scope. See the docs here.
However, rather than adding it as a system dependency it might be better to mavenize the jar itself, then you can build and install it into your dependency management system.
Also, see this question: Can I add jars to maven 2 build classpath without installing them?
You can use the systemPath attribute in the dependency tag in the POM file of your project.
In your pom.xml, use the following snippet corresponding to abc.jar:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>abc</groupId>
<artifactId>x</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>{path_to_abc.jar}</systemPath>
</dependency>
</dependencies>
The scope parameter corresponding to this artifact must be set to system, for the artifact to be picked up from the specified systemPath.
Hope this helps!
A normal maven dependency is always resolved by looking into a repository. So you must put your JAR file into a repository.
You could install your JAR into your local repository. Have a look at the install plugin. The install-file goal is your friend.
If other developers also need this JAR (because they are working with the same project), they either need to install it locally too, or - better - you deploy the JAR to a remote repository. Have a look at the deploy plugin. Here the deploy-file goal is your friend. For deploying artifacts, you need a repository manager like Nexus or Artifactory.
However, a dependency could also have the system scope (look at the other answers).

Embed local jar files in maven project

I have some local jar files from a non-maven project which I wish to include in my maven-based eclipse project.
These jar files are undergoing a lot of change as me and my project buddy attempt to 'fix' them, so I'd prefer not to upload them to a repository to avoid making a maven version of this non-maven project if this is possible.
Of course, the jar files need to be embedded in the resulting deployment jar. We did this before using Ant which let us specify that those jar files should be included.
How do you do the same thing in maven? Take into consideration that we do have maven dependencies too which all work fine and aren't required in the deployment. Some answers I've seen don't allow for this requirement.
Here's one of my attempts - the problem is that the jar does not get embedded:
<dependency>
<groupId>se.krka.kahlua</groupId>
<artifactId>kahlua-core</artifactId>
<version>5.1_2.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/kahlua-5.1_2.1.0-core.jar</systemPath>
</dependency>
System paths are a very bad idea. When anybody else checks out your projects, he cannot build it anymore. (I always see such crap in many companies). The right solution would be to install the jar into the local repository:
$ mvn install:install-file -Dfile=[JAR NAME] -DgroupId=[GROUPID OF
JAR] -DartifactId=[ARTIFACT OF JAR] -Dversion=[VERSION OF JAR]
-Dpackaging=jar
In your project, you just add the dependency as usual after you installed the jar into the local repository.
<dependency>
<groupId>[GROUPID OF JAR]</groupId>
<artifactId>[ARTIFACT OF JAR]</artifactId>
<version>[VERSION OF JAR]</version>
</dependency>
You can use maven-install-plugin to install kahlua-5.1_2.1.0-core.jar into the local repository then this dependency will behave as any other, see http://maven.apache.org/plugins/maven-install-plugin/usage.html. Or make a remote repository in a location shared with your buddy and let him upload his jar there with maven-deploy-plugin:deploy-file (http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html) each time he changes it and add this repository to your pom. You can use SNAPSHOT version if this jar changes often

Categories