Maven deploying a specific artifact - java

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.

Related

Adding Stanford CoreNLP 3.9.2 as Dependency via Maven

I'm trying to add Stanford CoreNLP 3.9.2 as dependency to my Eclipse/Maven project:
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.9.2</version>
</dependency>
Next to my POM.xml file I see a little red x icon. When I open POM.xml there is no additional information regarding the error.
When I click on Java -> Properties -> Java Build Path -> Maven Dependencies I see that the Jars that were expected to be added to Maven via this dependency are missing. This is odd because I regularly add dependencies this way without any error.
Apparently, something is preventing Maven from downloading the dependencies. What could it be?
Update:
I changed POM file to version 3.5.2 (instead of 3.9.2) and now all errors are gone.
If anyone can explain WHY this solved my problem (and how to make things work with version 3.9.2) I will accept it as the answer.
Update:
When I go to my Maven repository I see that most of the required Jars have been downloaded by Maven. For example, Maven repository will contain the folders: \\maven\.m2\repository\edu\stanford\nlp\stanford-corenlp\3.9.2 However the folder will not contain the Jar: stanford-corenlp-3.9.2 - but it will contain every other Jar such as stanford-corenlp-3.9.2-models and stanford-corenlp-3.9.2-sources etc.
This makes the whole situation even more confusing. If Maven is downloading the Jars why is it skipping just one Jar? I looked in several other folders (dependencies of corenlp) and I see similar phenomenon - it's always the main Jar of that folder that is missing.
What's worse, when I download and add the missing Jars manually to Maven folder, the (missing) text next to Jar goes away but there's still a little red x icon next to POM file. I have no idea what is going on.
Any insights?
Thanks!
I have no idea why this fixed the problem but in my POM file I had an entry:
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.debug.core</artifactId>
<version>3.13.0</version>
</dependency>
I update this dependency to:
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.debug.core</artifactId>
<version>3.14.0</version>
</dependency>
Now all errors have disappeared.

How to add 3rd party JARs to Maven project [duplicate]

This question already has answers here:
Add a dependency in Maven
(5 answers)
Closed 3 years ago.
My area of expertise is not Java. I develop in other languages on other platforms.
Right now I'm developing a series of Java servlets for a project. The servlets are to run on a CentOS server running FileNetP8.
I actually got all of the planned items finished and running.
Now when I am trying to add a few more services, the library I came across is available via Maven. I have no idea what Maven is. Read up on it today. Created a test project to try it out.
My development environment is Eclipse Photon on Windows 10.
Now my problem is I can't figure out how to add the Filenet JARs to the project. I can't upload them to some Maven repo. Searching the Web says to add them to the local repo, but I don't understand how to do that to the Local repo in Eclipse's builtin Maven.
I think that the JARs don't need to be packaged within the deployment WAR because the app will be deployed to the Websphere server that runs Filenet so they should be available on it. Should I add them as external JAR references to get the project to compile?
I provide below the following approaches to check based upon your suitability.
Approach-1: Install manually
If you have only 1 jar file, execute the following command by replacing as per your requirements.
mvn install:install-file \
-Dfile=<file path location> \
-DgroupId=<your own custom group id> \
-DartifactId=<your own custom artifact id> \
-Dversion=<some version number> \
-Dpackaging=jar \
-DgeneratePom=true
Once it is done, use the following in your project pom.xml in the dependency section.
<dependency>
<groupId>your own custom group id</groupId>
<artifactId>your own custom artifact id</artifactId>
<version>some version number</version>
</dependency>
The downside of the above approach is, only you can use it as it installs in your .m2 directory. For other developers, they have to follow the same approach. It is not a suggested approach.
Approach-2: Manually add the location of jar file
Add the following dependency in pom.xml
<dependency>
<groupId>some.group.id</groupId>
<artifactId>some.artifat.id</artifactId>
<version>some.version.no</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/yourActualJarFileName.jar</systemPath>
</dependency>
The downside of the above approach is, you have to put all the jar files in a directory and you have to provide the path. All the jar files should be part of your project, it means all the jar file should be put in source code repository. Although it servers the purposes, still it is not a good approach. If you got a newer/higher version of jar file, again you have to put it inside your lib directory. It means you have manage all the old and new versions of jar files.
Best Approach
Maintain Nexus or Artifactory, or any artifactory management system in the organisation and put all the jar files and provide the definition of your custom jar file. It will provide you pom definition. Based upon this, you have to add the dependencies in your project pom.xml. Here you can maintain n number version of your custom jar files.
Seeing as every answer involves maven i'll try to provide a different and somewhat old school approach. You could always import your jars directly into your project by right clicking on it -> properties -> Add Jars. Apply when done and VoilĂ . This is far easier than understanding the complexity of maven.
Dependencies need to be added in pom.xml under dependencies tag. Maven will download the specified jars from maven central repository for the first time, and it will be saved in your local repo.
If the dependencies are not available in central repo and if they have their own repo, you to need specify it in the repositories tag.
<repositories>
<repository>
<id>repo-id</id>
<name>repo-name</name>
<url>http://repourl</url>
</repository>
</repositories>
Any change in the pom.xml, maven will automatically download it.
<dependencies>
<dependency>
<groupId>dependency-id</groupId>
<artifactId>artifactid</artifactId>
<version>1</version>
</dependency>
</dependencies>
If the jars are available in Maven Central repo then all you hvae to do is add dependency under dependencies section in your pom.xml file. Set the scope as required, like you said jars don't needed to be packaged in WAR file as they might be available on server then you can set scope to provided.
<dependency>
<groupId>groud-id</groupId>
<artifactId>artifact-id</artifactId>
<version>version-numbe</version>
<scope>scope</scope>
</dependency>
If jars are not available in central repo and you want to install them on your local repo then below command should help you. More information here.
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

How to tell the project to download internal dependency?

I have a JAR. It is not a FAT JAR. It only contains my classes. But my JAR has a dependency upon azure-servicebus. I don't want to add azure-servicebus to my jar and make it a fat jar.
I just want that when the project adds my jar, it should download azure-servicebus automatically.
I am using Maven to create a jar (without dependencies).
How do I specify that? Is this possible?
edit:
I want the project that is adding my jar as a dependency should download azure-servicebus WITHOUT project having to add dependency for azure-servicebus or me packaging azure-servicebus within the jar file.
Assuming you already have a maven structue in your project, you would add the following line to your pom.xml:
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-servicebus -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>1.2.12</version>
</dependency>
If you don't have a maven structure, you'd need to generate one. This usually can be achieved within the IDE you're using.
If you want the resulting jar not having the service-bus, you can make the dependency some kind of compile only, which would be more or less a duplicate of this question: Is there a Maven "compiler-only" scope for dependency artifacts .
However, this would imply that your target runtime has to include the definition for the classes in some way (via -cp switch maybe).
From my POV: If you need a dependency to run your program, include it as long as the licence allows that.
If I understand you correctly, the structure you are talking about is:
some-project depends on your-jar which depends on azure-servicebus.
If some-project declares a Maven dependency on your-jar, then it gets azure-servicebus automatically as dependency because Maven does transitive dependency resolution. So when you build some-project, azure-servicebus will be on the class-path and if some-project is a WAR/EAR, than azure-servicebus will be part of that WAR/EAR.
I asked this question without knowing something very important.
When jars are put into artifactory, a corresponding .pom file also has to be placed alongside it (outside of the directory). This pom file is what tells the dependent project that the jar you are dependent upon, requires so and so dependencies itself.
This answer helped me understand:
https://stackoverflow.com/a/50002072/4828463
Thanks to everyone who tried.

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.

Categories

Resources