Maven copying Tomcat JARs into webapp WEB-INF/lib directory - java

I have a very simple webapp published as a WAR file, being developed in Eclipse. I'm using m2e for dependency management. My project, in the Project Facets tab, has the Dynamic Web Module enabled, along with Java. In addition, under my Deployment Assembly I have the Maven dependencies listed, but no inclusion/exclusion options:
My WAR builds fine, and runs fine in Eclipse. However, the Dynamic Web Module seems to be resulting in my WAR file containing a number of Tomcat JARs, in addition to all my other dependencies:
I was able to remove tomcat-catalina-7.0.30.jar (not pictured) by including in my POM.xml:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.30</version>
<scope>provided</scope>
</dependency>
But it seems there must be a way to tell Maven that the entire Tomcat container will be provided at runtime. I don't want to add ~20 provided entries to remove the rest of them.

Look at the POMs of your project. Somewhere, someone added them without the proper scope.
In Eclipse, you can use the POM editor. There is a set of tabs at the bottom of the editor. One reads "Dependency Hierarchy". Here you can search for "tomcat" and m2e will tell you which POMs contain such a dependency.

Related

Adding ojdbc to maven project

I'm creating a custom document library action in Alfresco Content Services 6.1.1 using alfresco-amp-archetype. I'd like to access data from external Oracle database.
I'm using ojdbc library from: https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc8/12.2.0.1
Since maven is unable to download the dependency on its own, I'm putting the jar in my project and adding it in pom (I've also added it in tomcat/lib directory):
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>8</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/ojdbc8-12.2.0.1.jar</systemPath>
</dependency>
When I call the action I'm getting "java.sql.SQLException: No suitable driver found for jdbc:oracle:thin"
How should I add the driver to my project for it to work?
The system scope that you are using is more meant to include things provided by java itself and is a deprecated feature.
The jar is not in the usual maven repositories due to license restrictions. So it needs to be somewhere with private access.
If you are not running a maven repository proxy like sonatype nexus or jfrog artifactory I would recommend that you copy the jar into your own maven repository: maven deploy into local repository
(probably best in a little script to repeat or share).
Don't store it in src/main/resources - everything in there will be added into the artifact you create. Choose another folder (like "dependencies" beside src) and then once copied into your local maven repository use that jar as normal dependency (remove scope and systemPath). The default scope is compile, so the jar will be included in your classpath so the driver should then be available (I assume you create some sort of war file?).
So there is also no need to manually add it into tomcat directly - but have it brought in via the war file.

What is the equivalent of adding a jar to deployment assembly in maven

I am working with a maven project in eclipse which I have been testing using the built-in "run on server" tomcat option. When I try to do this, I get errors stating certain dependencies are unmet from an external jar I reference in the POM. However I have found if I add the jar via the DeploymentAssembly Tab I can run in eclipse without issues:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.company.user.server.UserDetailsAuthoritiesMapper#0'
The issue comes when I try to deploy on an actual tomcat instance. The Maven build runs fine, but when I start the servlet I get the same unmet dependency errors. This to me is indicating that the external jar is not being properly packaged into the war. What is the maven equivalent of adding the package via the DeploymentAssembly tab in eclipse? The entry in the POM.xml:
<dependency>
<groupId>com.company.webapp</groupId>
<artifactId>webapp-user</artifactId>
<version>106</version>
</dependency>
Thanks
You need to locate the maven info for the external jar. If you google the name of the jar and maven you often find a direct link to the block you need. For example if I want version 1.58 of the Bouncy castle jar Google "Maven BouncyCastle" you can find the artifact info. Add that info to your pom.xml as a new dependency in your block.
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.58</version>
</dependency>
After you rebuild, refresh maven in your IDE 1st after doing a clean, this will tell maven to pull down the jar and added it to your build.
You can also go to the repo directly and search:
Maven Repo: https://mvnrepository.com/

Using third-party libraries in Eclipse RCP Tycho app

I've created a boiler-plate project following vogella's extensive Tycho tutorial.
Facts:
There's no feature, and there's no plugin. The only plugin is the RCP app, which is also the entry-point.
Problem:
I have no idea in which pom.xml do I include the 3rd party dependencies.
I cannot include them in the RCP project, because the packaging of that pom is eclipse-plugin, and not jar. From what I've noticed, if I change the packaging to jar, then the "Maven Dependencies" library is added automatically. If I change back to eclipse-plugin, they get removed.
Questions:
Where do I add the dependencies? There's no pom with jar packaging in my project.
Should I create a separate project with the necessary JARs? How do I include that dependency to my entire project?
Is it really that much of a good practice to create a separate plugin and a feature for this RCP app?
Related solutions:
"Update projects" doesn't work, and neither do the n other solutions in the other SO questions.
There's also this question and that question, but I don't fully get the answers
I think that you have a fundamental misunderstanding.
Maven: Maven determines all of the project dependencies via the pom.xml and resolves transitive dependencies automatically (assuming that all of the pom files and artifacts exist in repositories that you've configured and correctly declare their dependencies).
Tycho: The problem is that Eclipse already has its own project model based on product files, feature.xml files, and plug-in MANIFEST.MF files. Tycho leverages the Maven machinery for Eclipse, but the idea is that the pom.xml files just configure the Maven plug-ins and declare the packaging type. That provides an entry point for Maven, but then Tycho takes over. While Maven would normally build the dependency chain from information in the pom.xml files, Tycho is building the dependency change from information in the product, feature, and MANIFEST.MF files. You don't put any dependencies in the pom.xml files. Tycho also uses Eclipse p2 repositories (instead of normal Maven repositories) for finding dependent plug-ins that are not found in the local modules or target platform.
That's actually a benefit for many Eclipse developers since they've already set up everything properly in their Eclipse plug-ins, features, and products. They do not want to have to repeat all of the dependencies in the pom.xml.
Using Libraries in Eclipse plug-ins: In Eclipse, if you want to use a library that is not already packaged as an Eclipse plug-in, you have a few options. Your plug-in can include a set of JARs in a libs folder and then include that libs folder in the plug-in and runtime classpath (see the build.properties file). Another option is to create your own "library plug-in" that repackages a JAR library as an Eclipse plug-in. See also https://wiki.eclipse.org/FAQ_What_is_the_classpath_of_a_plug-in%3F. That's the answer that you're getting above.
The problem is that if you're trying to include a complex library with multiple JARs that is normally distributed and included in a standard Java project via Maven. We hit this problem with the Jersey JAX-RS implementation in my project. There's no p2 repository that includes all of the pieces of the libraries as plug-ins with correct dependency information.
Easy Solution: If you need a common library, check the Orbit project first to see whether the libraries have already been packaged as Eclipse plug-ins, http://www.eclipse.org/orbit/. In that case, you can download them and include them in your target platform, or you can pull them in dynamically at (Tycho) build time from their p2 repository. Your plug-ins would just include those plug-ins as dependencies (in the their MANIFEST.MF files).
Workaround / Solution: In our case, Jersey JAX-RS was not available as an Eclipse plug-in, and it had a bunch of transitive dependencies. The workaround was to create an Eclipse "library plug-in" like I mentioned above with two pom files. We initially created a skeleton plug-in with an empty libs folder. One pom file is just a standard Maven pom file with <packaging>jar</packaging> that declares the top-level dependencies required to pull in the Jersey JAX-RS implementation and all of its dependencies. The dependencies are declared with <scope>compile</scope>. We use the maven-dependency-plugin to copy all of those dependencies into the project's libs folder.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
We actually ended up running Maven with that pom by hand from time to time to update the libs, and then we just checked the plug-in with all of its dependent JARs into source control. Checking the build later, I see that we actually populate the libs folder on-the-fly with Maven with a separate build task just before we start the Maven/Tycho part of the build. Of course, plug-in's MANIFEST-MF file's Bundle-ClassPath and Export-Package entries are coming straight from source control. We have to check those from time to time to ensure that they match the libraries and packages that we're getting from Maven. (That doesn't tend to change much unless we bump major library versions or add a new dependency at the Maven level.) The plug-in's build.properties has the libs/ folder as part of bin.includes.
In the development environment, after we first check out the code, we just run mvn (with an External Tools launch config that's also checked in with the project) on the project's "copy dependencies" pom file. That populates the libs folder with all of the JAX-RS libraries and dependencies. We only have to run it again when we update something about the dependencies or when we're jumping between branches that have different versions of the JAX-RS dependencies. We set .gitignore to ensure that we don't commit the libs to Git.
The other pom for this project is set up like a normal Tycho pom file with <packaging>eclipse-plugin</packaging>. During our automated build, we run one step early in the build process (just after check out) that calls mvn with the jar pom to populate the libs. Then we proceed with the main Maven/Tycho build using the eclipse-plugin pom. The eclipse-plugin pom has no dependency information (as I said above). It's just providing Tycho a way to recognize the Eclipse plug-in and build it based on its MANIFEST.MF and build.properties files. But the built plug-in includes and exposes all of those libs that were populated by the mvn call to the jar pom step.
So, it's a bit of a mess, but that's the best solution we found a couple of years ago when we hit this problem. I'm not sure whether Tycho is doing any work to permit some sort of hybrid Maven/Tycho build that could do this automatically as part of the build. I guess I should ask the developers. :)
Your questions:
Where do I add the dependencies? There's no pom with jar packaging in my project. Answer: The workaround above lets you do it with one project. You just have two pom files, like pom_deps.xml and pom.xml. You just have to invoke the pom_deps.xml separately to populate the libs folder (in the dev environment and with your automated builds).
Should I create a separate project with the necessary JARs? How do I include that dependency to my entire project? Answer: the workaround that I described above lets you do it with a single project. Another way to do it is to create a separate JAR project, but I don't think that your Eclipse RCP app can really include a <packaging>jar</packaging> module in a useful way. The only way I've found to do it is to use a similar workaround. You build the JAR module first, install it into the maven repository, and then have one of your plug-in projects bundle the JAR in its libs folder. (If you really want to do it that way, ask. We have a case where we have to do that, too, and I can provide the steps we do in development and the build to make it work. I think the single project workaround that I provided above makes more sense for your case.)
Is it really that much of a good practice to create a separate plugin and a feature for this RCP app? Answer: that's really a separate question. If you have a feature with multiple plug-ins, you have the same problem. Tycho can handle the product/feature/plug-ins, but it cannot jump across into Maven-based dependency resolution. You'll end up having to use the same workarounds
Summary: The fundamental issue is that Eclipse plug-ins can't "see" a bare JAR library. The plug-in needs to have the library included in its local libs folder (with a matching Bundle-ClassPath entry in MANIFEST.MF), or it needs to depend on some other plug-in that exports the appropriate packages. Tycho just resolves dependencies via Eclipse plug-ins, and it cannot leverage normal Maven dependency resolution directly to pull in a bunch of JARs. If all of your dependencies are already plug-ins, you're fine. If not, you may have to use the workaround above to package a set of libraries for your plug-ins to use.
Just adding the plugin to pom dependencies and including the entry <pomDependencies>consider</pomDependencies> in the configuration of target-platform-configuration makes it work.
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho.version}</version>
<configuration>
<!-- The configuration to make tycho consider the maven dependencies -->
<pomDependencies>consider</pomDependencies>
<!-- other configurations -->
</configuartion>
</plugin>
<!-- other plugins-->
</plugins>
<dependencies>
<!-- An example third-party bundle (plugin) present in maven repository-->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.gogo.shell</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Reference link here.

Converting to Maven project and adding dependencies to pom.xml automatically

I started working on a Dynamic Web Project some time back and now I'm almost in the stage of finishing it. I have used a number of jars in my project which I have used by downloading the jars manually and then adding them to the build path in Eclipse
Now my requirement asks me to convert the project to a Maven Project which I did using Eclipse. The pom.xml is empty now.
Is there anyway I can automate the addition of <dependency> of the jars which I have used in my project to the pom.xml ?
Or do I have to manually search for each jar in the repo and then add its dependency in the pom.xml?
I have also customized some jars to suit my application. So I will skip adding them to the pom.xml obviously. But that jar has some dependencies on other jars which are used as it is; which can be obtained from the maven repo.
I could not find anything related to this on Google or Stack Overflow.

Maven deploying a specific artifact

So I am working with the Proguard Plugin to obfuscate one of my jars before deploying it to Maven. My goal is to have only the obfuscated jar deployed to Maven.
Currently I noticed that two jars get deployed, myJar.jar and myJar-small.jar (the obfuscated one from proguard). I have confimed that my Nexus index contains both of them and a single pom after being deployed. The problem is that when I add myJar as a maven dependency to another project, the unobfuscated jar is pulled into the project, not the obfuscated jar.
Is there way to only upload the obfuscated jar or to specify which jar should be pulled into other projects?
I have looked at the Proguard options here, but not been able to find much that helps.
For your own use, when you define your dependency, add the classifier tag with a value of small.
<dependency>
<groupId>myGroup</groupId>
<artifactId>myJar</artifactId>
<scope>test</scope>
<classifier>small</classifier> <!-- <<<<<< like this <<<<<< -->
</dependency>
For publishing, you can manually deploy the artifact with a new classifier as demonstrated here:
http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html
I assume you use the Maven ProGaurd Plugin (http://pyx4me.com/pyx4me-maven-plugins/proguard-maven-plugin).
If you leave the attach parameter on its default false value and do not set anything for outjar, the original artifact is overriden by the obfuscated one. Thus, only the obfuscated version will be deployed.

Categories

Resources