I am trying to understand a package of java code (let's call it mainPackage), which has an import from another package(Let's call this commonUtility). I can see the import statement, but cannot see the dependency directly in the pom file. I just need to understand this to make a few changes in the commonUtility so that it can be reflected in my mainPackage jar.
for example I can see a import statement in the mainPackage class file
import com.training.tdw.commonUtility.transform;
So I am expecting to see
<dependency>
<groupId>com.training.tdw</groupId>
<artifactId>commonUtility</artifactId>
<version>1.0.1</version>
</dependency>
But it is not present. So how will i find out the ink between the two packages. Correct me if my understanding is wrong.
It seems at least one of dependencies defined in your pom.xml depends on com.training.tdw.commonUtility:1.0.1
So, you do not need to specify com.training.tdw.commonUtility:1.0.1 dependency explicitly in your pom.xml. That is an essence of "Transitive dependencies" feature appeared since Maven 2.
Transitive dependencies are a new feature in Maven 2.0. This allows
you to avoid needing to discover and specify the libraries that your
own dependencies require, and including them automatically.
See: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
Please note that "test" scope is not transitive!
You can use the dependency:tree goal to find it. Try running
mvn dependency:tree -Dverbose -Dincludes=com.training.*
This assumes that the group id contains com.training, its possible however that its named something silly, you might just run a full dependency tree by removing the -Dincludes and manually looking.
Alternatively if your using an IDE you could navigate to a class inside that package and there should be an option to show it in your project/package explorer. From there you could determine the JAR file name and that should correspond to the maven package artifact id
A jar can have an arbitrary GAV (groupId, artifactId, version), and it need not be connected to the package you import. Sure, many people start their package name by the groupId, but others don't so in general there is not direct way to infere the necessary (or used) jar from a given package.
If you are using Nexus 2 (or MavenCentral), there is a feature named "Classname search" which allows you to search for a (qualified) class name and get as a result the jars that contain such a class. Unfortunately, Nexus 3 has dropped this.
Related
I know I am asking the very popular question. But I can not find the solution to the problem. I have a sandbox to which I added a code of the unit test MulticurveBuildingDiscountingDiscountAUDTest.java file and commented it.
Then I added the main method and I could successfully run the program (print something in a console).
Finally, I uncommented the code of the MulticurveBuildingDiscountingDiscountAUDTest.java file and I saw the following error:
The import com.opengamma.analytics.financial.instrument.index.GeneratorSwapFixedONMaster cannot be resolved.
And further in the code:
GeneratorSwapFixedONMaster cannot be resolved
I know that this import is located in the og-analytics src/test/java location, which I believe is not listed anywhere in the build path. I believe the problem is with a build path options and specially with classes like GeneratorSwapFixedONMaster which were created specially for tests. I have been playing around with cleaning, rebuilding projects, reinstalling and as a result updating the JRE. I have visited these Import *** cannot be resolved [duplicate] and these Eclipse error: “The import XXX cannot be resolved” questions.
Do you know what shall I do to cure the following error?
I have many problems with other imports from the original MulticurveBuildingDiscountingDiscountAUDTest.java file as well.
Update: #1 is a location of my file. #2 is the location of classes this project uses. The MulticurveBuildingDiscountingDiscountAUDTest.java file is taken from the src/test/java
Update 2: one may see that in Libraries I have included all the dependencies I might need (at least I do not know what else to add). The Maven Dependencies contains the hole og-analytics package:
You included the source (src) folder og-analytics/src/main/java which contains the *.java files instead of the classes (bin or classes) folder with the *.class files (in your case, probably og-analytics/target/classes).
But instead using Add Class Folder... you should add the project og-analytics in the tab Projects. Or even better, in the Maven pom.xml file add the dependency to the project og-analytics like you did for og-util.
I know that this import is located in the og-analytics src/test/java location, which I believe is not listed anywhere in the build path.
Perfectly explains your problem. In order to import any class, you must either have the source in your build path, or some directory that contains a compiled version of that class. It is that simply.
The answer is: get clear on your project setup. If you intend to use classes from somewhere, you have to make them available somehow. Otherwise it will not work. In your case: if your tests want to make use a certain thing - then you should probably add that "thing" to your test project (you should avoid putting test-only stuff to your "product" projects).
That is all there is to this.
Right now i am trying to do some POC with JBoss Drool Workbench.
I am facing one Problem: I have uploaded on JAR file as a dependency. which contains Bean and some of the business logic service class. which also contains class which have list of static method which need to call from Rule.
My Problem is i am able to get all beans and constant classes in Guided Rule UI in Config tab for Imports. but other then all remaining class, like class contains static method are not available for import. Due to that i am not able to import that class, and not able to use that one.
Please help me out.
Genius Thanks in Advance.
After seen warning i came to know that i also need to upload dependent jar file in workbench and also provide dependency to project, drool itself provide a hint for same. finally the problem has been resolved.
In the project setting tab, you have to add dependency of the maven project containing POJO. It will give you option to either load from central or load from local repository. If you choose, central, the pom.xml will contain the reference to download the dependency while createing the jar. If you choose from local, it will add the class files to your jar.
I'm having trouble including a JAR file that adds a class that will let my main class send emails.
What I have done...
Saved the EmailAPI.jar file in my folder, as mentioned in below POM text (and it finds it, because in NetBeans I can explore the class)
Updated the dependency in my POM file, as follows:
<dependency>
<groupId>EmailAPI</groupId>
<artifactId>EmailAPI</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}\src\lib\EmailAPI.jar</systemPath>
</dependency>
Add the import (NetBeans automatically added this when I used the Email class, so it seems to know where to look...)
import me.nrubin29.emailapi.Email;
Call the class, directly using the structure provided
//send an email
new Email()
.withSMTPServer("smtp.gmail.com")
.withUsername("xxxxx#gmail.com")
.withPassword("xxxxxxx")
.withTo("xxxxx#gmail.com; xxxxx#gmail.com")
.withSubject("[RP] Server has started")
.withBody("This is the body!")
.send();
I can build fine, it all works out... but then when I try to run it (as a plugin to Minecraft), I get a NoClassDefFoundError, as shown here:
http://pastebin.com/V33gCLVG
I don't understand what I'm missing here. Can anyone point me in the right direction?
You use <scope>system</scope>. Is it available in the Minecraft enviroment? See Maven, Introduction to the Dependency Mechanism, Dependency Scope: "This scope is similar to provided [...] " and under provided: "indicates you expect [...] a container to provide the dependency at runtime."
It means that you are missing the jar in your runtime environment. You might need to change the scope of your maven dependency to compile.
EmailAPI requires two JARs in order to run. I think they are activation and mail or something. I can look at the project but I think you might be missing them.
I am using maven to build my project say utils_java.
The project structure is typical src/main... and src/tests....
I have a class in this package and needs some sophisticated setting and use of JNI etc. So to facilitate the unit testing I have some other package called mock_java containing a pseudo/proxy/mock class with same name and package structure.
pom.xml entry:
<dependency>
<groupId>com.test.my_mock_java</groupId>
<artifactId>my_mock_java</artifactId>
<version>0.0.1.14244.5</version>
<scope>test</scope>
</dependency>
I also checked using other maven-antrun-plugin to check the class path. Test class path contains the jar for this mock_java pkg.
This is not working for me. In case there 2 two packages we can control their order is classpath by placing them at proper place in pom.xml. But what should be done in this scenario where a class in current project needs to be shadowed by one from other package only for testing purpose ?
Relying on the order in the classpath in order to shadow classes is just asking for classes. There too many factors you don't control. Without more information on your actual use case it is, however, hard to give any better suggestions.
Did you try to solve the problem by defining more profiles ?
You could define the following profiles:
production (activated by default)
test
In this way you have possibility to include/exclude dependency based on them
I am writing a Maven plugin and I would like to automatically resolve specific dependencies and add them as dependencies to the project based on the parameters given to the plugin.
I have been able to successfully resolve dependencies through aether, but there seems to be a disconnect between aether and the MavenProject.
There is a method on MavenProject#addAttachedArtifact which I'm guessing is what I want to use. However, it takes a org.apache.maven.artifact.Artifact while the one retrieved from aether is org.sonatype.aether.artifact.Artifact. I found a plugin that has a conversion method between the two, but I figure there ought to be a more standard approach.
I have also tried using the DefaultArtifactFactory to create a org.apache.maven.artifact.Artifact but get a NullPointerException when trying to get an ArtifactHandler.
code:
DefaultArtifactFactory factory = new DefaultArtifactFactory();
Artifact mavenArtifact = factory.createBuildArtifact("com.beust", "jcommander", "1.27", "jar");
result:
Caused by: java.lang.NullPointerException
at org.apache.maven.artifact.factory.DefaultArtifactFactory.createArtifact(DefaultArtifactFactory.java:155)
at org.apache.maven.artifact.factory.DefaultArtifactFactory.createArtifact(DefaultArtifactFactory.java:117)
at org.apache.maven.artifact.factory.DefaultArtifactFactory.createArtifact(DefaultArtifactFactory.java:111)
at org.apache.maven.artifact.factory.DefaultArtifactFactory.createBuildArtifact(DefaultArtifactFactory.java:75)
at com.foo.bar.module.IncludeModuleFrontEndMojo.execute(IncludeModuleFrontEndMojo.java:165)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
... 20 more
So really, these are the things I've tried, a resolution to these issues would be great, but I'm really after the right way to do this. Any ideas?
UPDATE
I wrote my own conversion method between the two classes:
private static org.apache.maven.artifact.Artifact aetherToMavenArtifactBasic(Artifact artifact, String scope, ArtifactHandler artifactHandler) {
DefaultArtifact mavenArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), scope, artifact.getExtension(), artifact.getClassifier(), artifactHandler);
mavenArtifact.setFile(artifact.getFile());
mavenArtifact.setResolved(true);
return mavenArtifact;
}
and found that the MavenProject#addAttachedArtifact method is to attach an artifact to an existing artifact (i.e. attach sources/javadocs jars to an artifact), which is not my goal. Instead I got the artifacts from the maven project and add my artifact:
project.getArtifacts().add(mavenArtifact);
which adds my artifact to the project (my artifact is then shown when I call the project's getArtifactMap() and getCompileClasspathElements(). However, this change does not persist. This is the problem I was really worried about. So the question has evolved into:
Can I make changes to the MavenProject and have it persist?
I don't think this is possible and for my purposes I decided instead to require the user to add the dependency in the project's pom file (and error out if they don't have it).
It seems to be by design that you don't allow the user to muck with the project configuration through a plugin to a point where you could break the build. I found a good post on advanced MOJO development here. A quote from it:
If this parameter could be specified separately from the main
dependencies section, users could easily break their builds –
particularly if the mojo in question compiled project source code. In
this case, direct configuration could result in a dependency being
present for compilation, but being unavailable for testing. Therefore,
the #readonly annotation functions to force users to configure the
POM, rather than configuring a specific plugin only.