Move class to another project with all of its dependencies - java

I need to transfer a Java class to a new Java project, with all of its dependencies.
Meaning I may have class a, which class b calls it, and class c calls class b, etc.
In this case, I will have to transfer classes a, b and c.
Is there a way to do this for all of the projects?
My problem is that I can't download any files to my computer. I use Intellij as IDE.

Have you tried dependency management tools like Maven or Gradle? A quick Maven tutorial https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
With Maven you can just create a Maven project from your IDE and provide it with a pom.xml that specifies how you wanna build the project and all its dependencies. For each dependency you specify, Maven could transitively pull other dependencies for you.

Related

Maven modules and dependencies - functional test a library

The ultimate goal is to find out a way to functional test a library.
I have a parent project A with sub modules B,C and D.
Let us assume that the module C has a dependency on module B (B being the library) and is included as a maven dependency using the <dependency> tag.
I am just curious to know when I start up the application server say Tomcat would a jar for B (along with others) be created and then C uses this jar?
I tried to monitor my directories to see whether jars were being created every time the server was started up but that doesn't seem to be the case.
If B isn't used as a jar then it would not exactly be a FT since the ultimate goal is to be able to use B as a library in other projects as well. I feel like this shouldn't be a problem since the code itself does not change but I'd like to get some insight into the same and comments if any. Thank you !
I hope I get it right, because you seem to mix building, deploying and testing
Maven will only build the current pom. It will not build dependency libraries but expect to find them in the repository.
Only if module A is a multi-module build, then B, C and D will be built when you build A.
Starting a tomcat will not trigger any build.
For a functional test you would build, package and deploy and execute your tests against the full application.

IntelliJ: How do I override a dependency in SBT with a module imported from local source?

I have 2 projects, Project A and Project B. Project A is a Play2 App that depends on Project B (which is a client library.) Currently Project B is pulled from our artifactory with SBT. I would like to set up IntelliJ such that Project B is pulled from the project source on my computer, rather than from the artifactory that is specified in.
I have added Project B as a module of Project A and added Module B to the dependencies of Module A. I then ordered Module B to be at the very top of the dependency list. The static analysis of the code seems to be working fine, there's not compilation errors showing when I updated Project A's code to use a new method signature that I've updated in Project B. However, when I run the Play App I get a compilation error stating that the method signature is incorrect.
Is there a way to override the module used at runtime for SBT and the Play App?
You can do this via sbt. In your build.sbt, for example:
val localDep = ProjectRef(file("/Users/me/projects/b"), "b")
dependsOn(localDep)
IntelliJ will import this dependency as a module. You should remove the library dependency however, to avoid conflicting classpaths.
Naturally, this makes the project hard to share unless other developers have the project in the same location. In that case, I would create a multi-project build instead, which is usually the best choice for tightly coupled projects with individual resulting artifacts.
Another option is a git project dependency:
val projectDep = ProjectRef(uri("git://github.com/me/b"),"b")

maven not picking dependencies from my local repository

I am working on a module (let's say A, version 1) that is available in my maven global repository. I have now modified the code inside this module and bumped the version up to version 2.
Now, the code for this module has not been committed and the global repository doesn't now about this new version. However, I have built the project and version 2 is available in my local .m2 folders.
I am now working on a new module (let's say B) that depends on module A, version 2. maven is not picking up this dependency on module A. And my code is not linking to classes in module A.
Any help would be much appreciated.
EDIT: I have performed a clean install of module A, and I can see the classes in the local repository.
Best,
Pulkit
Well, if you are using an IDE, which I presume you do (you are using maven), you could try to update the project configuration and update the dependencies. Also make sure you refer to the correct versions of your modules in your pom.xml file(s) . This is all presuming you have performed a clean install of your 'A version 2', of course. Good luck!

Netbeans running multimodule Maven project

I've recently tried to work with NetBeans and I don't understand how it handles Maven multi-module projects.
My project has fairly complicated modules structure and when we're working on it (we mainly use IntelliJ community edition) we don't want to open all its modules (~50 modules) because it will take hours to load the project, instead we've created a 'workspace' module: a folder with pom.xml that has a packaging type pom and defines modules that I would like to load.
Lets say it defines modules A, B, C.
We have our main method (we don't use any type of container) in module A which is a low-level infrastructure module.
As a runtime dependency we need A, B, C. But A doesn't really depend on B, C, but rather B and C depend on A (in terms of Maven dependencies).
So we've created another module, lets call it runner, where we define all the dependencies. Our workspace pom.xml has module declaration of runner, so in order to run the project from IntelliJ, we use a 'classpath of module runner' while running method main in module A.
Now, How can I achieve the same affect with the latest Netbeans (7.1.2)?
I understand that question is kind of newbie's style, but I struggle with it a lot of time with no luck.
Just struggled with the same issue - from what's listed in the Netbeans wiki it seems that every module represents it's own Netbeans project and everything else would be expressed with project dependencies. See http://wiki.netbeans.org/MavenBestPractices
A project with modules is known as a multimodule, or aggregator project. Modules are projects that this POM lists, and are executed as a group. The Maven projects in NetBeans lists these modules as "Required Projects". So you will only get the required projects list populated for Maven projects with "pom" packaging that have modules defined.

Help me understand making maven project w/ non-maven jar dependencies usable by others

I'm in the process of learning maven (and java packaging & distribution) with a new oss project I'm making as practice. Here's my situation, all java of course:
My main project is ProjectA, maven-based in a github repository. I have also created one utility project, maven-based, in github: ProjectB. ProjectA depends on a project I have heavily modified that originally was from a google-code ant-based repository, ProjectC.
So, how do I set up the build for ProjectA such that someone can download ProjectA.jar and use it without needing to install jars for ProjectB and ProjectC, and also how do I set up the build such that someone could check out ProjectA and run only 'mvn package' for a full compile?
(Additionally, what should I do with my modified version of ProjectC? include the class files directly into ProjectA, or fork the project into something that could then be used by as a maven dependency?)
I've been reading around, links such as this SO question and this SO question, but I'm unclear how those relate to my particular circumstance. So, any help would be appreciated. Thanks!
So, how do I set up the build for ProjectA such that someone can download ProjectA.jar and use it without needing to install jars for ProjectB and ProjectC
Assuming ProjectA is a JAR, you can create an executable JAR that bundles the dependencies with the Maven Assembly Plugin (and the predefined jar-with-dependencies descriptor) or with the Maven Shade Plugin.
how do I set up the build such that someone could check out ProjectA and run only 'mvn package' for a full compile?
You have to deploy the dependencies to a repository that can be read over HTTP and to declare this repository in your pom.xml. AFAIK, git-hub doesn't offer any facility for that. But any web hosting service with FTP access (or better, scp) should do the trick. If your project is open source, another option would be to use Sonatype's OSS Repository Hosting service.
Just in case, you might want to read this blog post but you won't learn much more things.
The easiest would still be to organize the 3 projects as a multi-modules maven project and to build all modules.
Additionally, what should I do with my modified version of ProjectC?
From a modularization point of view (assuming you found a solution for the above part about repository), it would certainly make sense to have it as a separate module, especially if there is an opportunity someone can use ProjectC outside your project.
You have to publish the code from the additional dependencies. Two options:
Use the maven-shade-plugin to create a maven artifact containing all the content of the B and C jars, and publish that under your own G/A/V coordinates.
Publish copies of B and C under your own G/A/V coordinates using the maven-deploy-plugin to your forge just as you will publish your own code. Different forges have different policies; but if you abide by the licenses of B and C you should be OK.

Categories