Setting custom Class Path while executing mvn exec:java - java

I have a simple use case of running a main method from a jar . This jar get created as a part of mvn packge and gets stored in myproject/target/myproject-jar-withdependencies
How i am running it right now is using a simple java -cp {path-to -jar} {mainclass} . I am trying to use mvn exec plugin for this but am having problems running it
The crux is that i need to force maven to find the main class inside a jar that is in target/
directory . I tried using -cp and jar using the argument tag but unfortunately that doesnt work either . Maven doesnt seem to recognize the jars inside the target directory as belonging to the class path
I need to specify the custom classpath in the exec plugin (the docs in mvn exec dont give much information to this)
Any pointers is much appreciated

Answered in comments:
Sorry about the very late update. But this is fixed. My pom was
misconfigured – Nitin Feb 8 '13 at 23:47

Related

Using particular clojure java-wrapper with no project.clj

I try to avoid asking questions that won't help many other people, but I've been at this for far too long to not ask for help. I'm trying to use this clojure wrapper for a java package as a dependency in my project, but I'm hitting a lot of errors because there's no project.clj.
I've tried using the lein-git-deps plugin with the following keys in my project.clj:
:plugins [[lein-git-deps "0.0.1-SNAPSHOT"]]
:git-dependencies [["https://github.com/lunkdjedi/clj-ta-lib"]]
Running $ lein git-deps gives me the following:
Setting up dependency for [https://github.com/lunkdjedi/clj-ta-lib]
Running git pull on .lein-git-deps
Running git checkout master in .lein-git-deps
but nothing changes in my local directory, except now there's an empty .lein-git-deps/ dir. I'm assuming this didn't work because of the missing project.clj.
I've been able to clone the project and call $ mvn package clojure:repl which successfully compiled and opened a REPL instance, in which I could use the wrapper perfectly. When I run $ lein install it gives me:
Couldn't find project.clj, which is needed for install
but when I run $ mvn install it gives me a BUILD SUCCESS and can be found in ~/.m2/repositories/clj-ta-lib.
I put :dependencies [[clj-ta-lib/clj-ta-lib "0.0.1"]] in my project.clj and restart the REPL but when I call (use 'clj-ta-lib.core) I get:
CompilerException java.lang.Exception: namespace 'clj-ta-lib.core' not found, compiling:(*cider-repl app*:71:13)
Edit:
No longer getting the previous error, but still getting this one:
CompilerException java.lang.IllegalArgumentException: No matching ctor found for class com.tictactec.ta.lib.meta.PriceHolder, compiling:(clj_ta_lib/core.clj:13:3)
The stuff you say you've done sounds pretty reasonable. It's a shame the project isn't hosted on clojars or on maven central, but at least there's a pom to allow you to publish or install it easily yourself. mvn install is the right thing to do, and when I clone the project myself and install it, everything installs fine just as you say it does.
However, I can't reproduce your error when trying to use the library. After installing it, I created a new project that depends on the just-installed artifact, and in a repl inside that project, I can use the library's classes just fine. Or, well, I don't know how to use these functions as intended, but the functions are available for my use:
user=> (use 'clj-ta-lib.core)
nil
user=> ta
#<core$ta clj_ta_lib.core$ta#c668834>
So I'd say, make sure you really did follow all the steps you say you did, and that your repl has been started cleanly inside of the project that depends on your library.
As mentioned here, https://mvnrepository.com/artifact/com.tictactec/ta-lib/0.4.0 just add [com.tictactec/ta-lib "0.4.0"] to the dependencies
in your project.clj.
:dependencies [[com.tictactec/ta-lib "0.4.0"]]

How do I know which project is requesting a specific jar from Maven

I'm using Eclipse and recently upgraded all my projects to use the latest version of a library.
However in the Maven repository I can still see the old version of the library.
I've deleted manually the old library from the Maven repository, but it keeps coming back.
I am sure all the projects in Eclipse point to the new version: I've checked all my pom.xml, I've used the "Dependency Hierarchy" tool, etc.
Is there a way to know which project is telling Maven to download the old version of the library?
Many thanks!
You can use the Maven dependency plugin's tree goal:
mvn dependency:tree
and filter using the includes option which uses the pattern [groupId]:[artifactId]:[type]:[version].
Re: "and I have many". Perform the following in the topmost directory:
find . -name "pom.xml" -type f -exec mvn dependency:tree -f {} ';' | grep '^\[.*\] [-+\\\|].*'
Syntax details may vary from Bash to Bash.
Hint: Try it in a bottommost project directory first to ensure that it runs properly as intended. Since you have many projects it may take a while to finish and to recognize possible errors only then.
You can use below command to get a tree of all dependencies and then find out where the specific artifact is coming from.
You can pipe with grep to show only the related ones if you you are on linux/unix based os.
mvn dependency:tree
Thanks guys, appreciated, but it certainly is not an easy way. It looks like you have to do project by project (and I have many). Plus most of my pom reference poms in other folders and it's not able to process that either.

How to execute a Maven build Java application

I am playing to Maven and tried to built a simple HellowWorld application.
This application uses Spring to libraries.
When I tried to run it, I run it through:
\target\classes
with command:
java -cp HelloWorldApp
It has a long list of classpath dependencies.
I think maven must have some more clever ways to do this instead of listing a whole list of dependency libs.
Can someone help?
Update:
Thanks. I now have another question. I run the project using:
mvn exec:java -Dexec.mainClass="com.vaannila.HelloWorldApp"
However, my project uses a Spring config called beans.xml which is in the
\src\main\resources
When I run it, it says:
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.jav
a:336)
How can I specify where to look the Spring config?
Many thanks
Run your application by issuing mvn exec:java at the command line, maven will take care of the rest including download of the maven exec plugin.
EDIT As for your updated question: It appears that maven did not copy your resources to the target folder, you can use the maven-resources-plugin to do that. This link should help you get this done.
If you use IDE, such as eclipse +m2eclipse - it will calculate all dependencies from maven dependencies.
If you are running from command line use Exec maven plugin

Setting groovysh classpath from a pom

I have a java project (not using groovy) but I'd like to interactively play with my java classes within groovysh. Is there an easy way to use the pom from my project to set the classpath of groovysh?
Just for the record, I've found a way to do it which I consider much more elegant when the project uses the gmaven-plugin: run mvn groovy:shell and you're ready to issue commands with the same classpath available to groovysh as the one available to the project in question!
MOP might help:
Scripting Goodies
Other times, you just need need the
CLASSPATH so you can use it in a
manually crafted script your running.
Try this.
mop classpath org.apache.camel:camel-example-pojo-messaging
Update: The above command can be used to output the classpath of an existing maven artefact. For example:
$ ./mop classpath org.hibernate:hibernate-core:3.3.2.GA
Prints the following output:
/home/pascal/opt/mop/repository/org/hibernate/hibernate-core/3.3.2.GA/hibernate-core-3.3.2.GA.jar:/home/pascal/opt/mop/repository/antlr/antlr/2.7.6/antlr-2.7.6.jar:/home/pascal/opt/mop/repository/commons-collections/commons-collections/3.1/commons-collections-3.1.jar:/home/pascal/opt/mop/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar:/home/pascal/opt/mop/repository/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar:/home/pascal/opt/mop/repository/javax/transaction/jta/1.1/jta-1.1.jar:/home/pascal/opt/mop/repository/org/slf4j/slf4j-api/1.5.8/slf4j-api-1.5.8.jar
That can be used somewhere else. As I said, it might help but I'm not 100% sure it will suit your needs (it seems the artifact needs to be deployed in a remote repo).
You can add them to classpath with -cp e.g.
groovysh -cp some.jar:another.jar

Ant scp task not working, even with jsch on ant/lib

I need to copy a war file via scp.
I have added the jsch-0.1.42.jar to $ANT_HOME/lib but I'm still getting this error:
Cause: the class
org.apache.tools.ant.taskdefs.optional.ssh.Scp
was not found.
This looks like one of Ant's optional components.
This is the result of running ant -diagnostics just in case:
http://gist.github.com/320859
I had the same problem and fixed it!
Remember, you need BOTH ant-jsch.jar (native from ant or java installation) and updated jsch (in my case it was jsch-0.1.46.jar) in ant lib dir.
You need to load the newest jsch.jar from http://www.jcraft.com/jsch/ and add to libs folder (but not replace ant-jsch.jar).
The mistake a lot of developers do:
ant-jsch.jar out of date (in this case <scp> task is unknown for ant)
Adjust libraries versions, so ant-jsch.jar and jsch are compatible.
jsch not exists or out of date in ant lib dir
Native ant-jsch.jar replaced with jsch (i did it...)
In last 2 cases ant knows scp command, but says it is not available.
This looks like your problem, from the top of the diagnostics:
optional tasks : not available
Your $ANT_HOME/lib directory is missing ant-jsch.jar, which is where your missing class comes from.
I'm not sure how this happened - I always install Ant from the ZIP file on the download site, which includes all the optional tasks. If you installed Ant from a Linux package, try checking to see if there is a separate package for the optional tasks.
Once you get the optional tasks installed, I would delete the old version of jsch.jar as #trashgod suggests.
I added ant-jsch and jsch dependencies in maven pom.xml or including those two jars in build solved the issue.
Execute the following command:
sudo yum install ant-jsch.noarch

Categories