Separate class paths for test code and main code - java

I have two source trees in my Java application in Eclipse. One is "src", and one is "test". Each one contains a directory tree of .java files. So far, so good.
However, I would like the classes in "test" to be able to see the classes in "src", but for the classes in "src" to not be able to see the classes in "test". Similarly, I want to include some test libraries. I don't want any of those libraries to be available in the "main" tree, lest I accidentally use one without thinking.
Is there an easy way to set this up in Eclipse?

There is no way to solve this in Eclipse without splitting the two source trees into separate projects (with one depending on the other). Maven could handle that at build time though.

You don't need separate projects if you use IntelliJ. You just specify application and test packages. It's possible to mark libraries as part of application and test as well.
Eclipse ought to have equivalent buried under all those plugins. If not, IntelliJ has a community edition now.
Or just use Ant. Personally, I prefer it to Maven. I find Maven to be too complex for such a simple job.

Related

what is the equivalent project file for a Java project?

What is the equivalent project file for a Java project? For example, a C# project file is denoted by the delimiter of .csproj. So what would it be for a Java project? Is it dependent on what IDE I am using for Java?
I did some search online about this and it seems to me that the answer to this question is dependent on the IDE. From memory, it seems to me, that all I have to do is drop the Java files into an IDE and then, somehow, a project file is automatically generated. I do not want to be steered in the wrong direction on this and so I want to first ask around for guidance.
You're probably looking for the build file, not the IDE project definition.
Is it dependent on what IDE I am using for Java?
Yes. IntelliJ has .iml files, and Eclipse has .project (that's not a file ending - that's the file), as well as .classpath, .factorypath, and more. But, many of these are effectively generated or just refer to the build file. In C that tends to be called Makefile, in the Java ecosystem, Gradle and Maven are the 2 most popular build tools; they have build.gradle and pom.xml respectively. These would be more useful as basis for knowing how a project is 'put together', so to speak.
From memory, it seems to me, that all I have to do is drop the Java files into an IDE and then, somehow, a project file is automatically generated.
That's very basic and should rarely work except for the simplest projects. For example, most Java projects have source files, test files (also source files but they are not part of the distribution), and resource files (non-Java files that are also needed for the app; think about the images for the icon on a button), as well as a ton of dependencies (third party libraries used by the Java app).
Given that folks like many IDEs, the IDE 'config file' is not what you are looking for, and many projects don't even check these into source control (and probably shouldn't). Together with the source, you have a build file. This build file knows how to download dependencies, compile every artifact (there doesn't have to be one; maybe there's the test code to the built, the main app, a plugin for some other tool, and an installer. Build tools can handle all that), run the tests and report on them, possibly even tell you about code coverage, and they usually can run the app, but more generally, you just want to tell the build tool to build a distributable for each relevant artifact.
Those build files are what you're looking for. With those you can build your project, or point an IDE at them and then you can edit the project with all the dependencies and classpath linkages all worked out for you.

Can I create an Eclipse Project that produces multiple executable Jars?

I have a project with a library JAR and a set of applications. Right now the applications (all 5 of them) are in a single project. These tools each have one or two classes, they run from the command line with one exception. Even the GUI tool has only about 8 classes. I am struggling with how best to structure this in Eclipse.
I have seen that I can use maven-shade to create multiple executable JARs from one project. I would need to convert the project to Maven, but I assume that is not a big problem.
I can create a separate project for each tool. This seems like overkill to have several projects with one or two classes in each.
Additionally, both of these solutions will leave me with either a bare class file or a JAR plus the dependent library JAR. Not ideal for deployment.
Before I pick one of these, I thought I would ask here if there is a different approach that I am missing for packaging these tools.
In eclipse, Export --> Archive file, You can select/deselect what classes you want to export into your Jar File
Yes, but You could/need to create using Maven, and for each build building jar you need to set the main Class.
Checkout the documentation of Maven.
I think You could to the same with Gradle, but not sure.
https://maven.apache.org/plugins/maven-jar-plugin/examples/attached-jar.html

Merging 2 jars into one avoiding duplicate classes

I have two netbeans project and i need to merge the 2 output jars in only one "big" jar. The problem is that these 2 project share a lot of classes and this is obviously a problem. I'm already using ANT with the zipgroupfileset attribute but it copy all the classes. Is there a way i can choose all the classes from project 1 THEN the missing class from project 2? thank you very much for ur help
Edit: i'm forced to have 2 project (sometimes i don't even have the second project but just the jar) so i kinda have to make an ANT script i think. Is strange that there is no attribute like don't copy the already existing class
Duplicate code is error prone. There are a few ways to resolve this duplication.
Project dependency. Have one project depend on the output of the other.
Combine the projects. If you are creating a single jar anyways, these projects might be candidates for merging into a single project. You can still manage code separation through modules.
Third project that both depend on. Often we find ourselves using the same StringUtils or the likes among all of our projects. Instead of maintaining code in multiple locations you can pull out the util/misc code into another project that both of the other projects share.
Rename classes. If for some reason the classes are named the same, but are not identical, then you need to decide if they are two things that should be namespaced/named differently, or if you want to merge and maintain 1 copy.
Eclipse make it for you.
You can create one new project in eclipse, import yours jars inside project and export him.
When you select Export / Java / Runnable Jar, check the option "Extract required libraries into generated jar". Just one copy of each jar are copied.

How can I make Java code accessible to two projects

A Java newbie here. I have created a small desktop application in IntelliJ IDEA 11.1. It consists of several java files with classes plus the main application file Main.java. I would like to make another application, which uses the same classes but a different Main file. I want to be able to change the classes source code from both projects. Therefore I'd like to have a structure like:
IdeaProjects\lib/myclasses\*.java (or similar with additional src if recommended)
IdeaProjects\project1\src\Main.java
IdeaProjects\project2\src\Main.java
How do I do it properly? I've read a lot about modules, libraries, jars and am just lost at the moment :(
I don't use IntelliJ so I can not help you specifically but You should create one project containing the shared resources and per program another project containing you Main files. Compile this first project into a jar file so you can use it as a library in the projects containing the main files.
In eclipse You can add other projects to your classPath. Maybe this is also possible in IntellyJ.

Setup sonar-runner for multiple java projects

I am trying to run sonar-runner to analyze multiple Java projects in one go. According to the documentation it is just a matter of creating a sonar-project.properties file for each project. But it is not clear to me where exactly I have to put these sonar-project.properties files.
I tried to add multiple .properties files in the $SONAR_RUNNER_HOME/conf folder but the runner does not seem to pick them up. It only sees the sonar-project.properties file.
Any suggestions on how to run sonar-runner for multiple projects?
Actually it is quite simple to let the sonar runner analyze multiple projects as long as they are in the same file system. Just put a properties file in a directory that is not to far away from the projects. Then declare each of your projects in this properties file.
Lets assume you have 4 projects in dev/general/BasicStuff, dev/service/CoolStuff, dev/utility/UtilStuff and dev/display/FrameWorkStuff.
As described in hereWay #2 you create a file in dev which contains the line
sonar.modules=BasicStuff,CoolStuff,UtilStuff,FrameWorkStuff
And for each of the "modules" a line like
BasicStuff.sonar.projectBaseDir=general/BasicStuff
CoolStuff.sonar.projectBaseDir=service/CoolStuff
Inside the project directories you in turn create a file which contains the other needed information, e.g.
sonar.projectName=BasicStuff
sonar.sources=src
If you start sonar runner with the top level properties as target you get a comprehensive result which shows metrics across the projects as well as allowing you to drill into each of them.
Hope this was what you were looking for.
The Sonar runner supports projects with multiple modules. It doesn't support the analysis of multiple projects in one invocation.
The Java runner needs to be run against each project, picking up the properties file in each project directory.
Since 2.0, Sonar runner works with multiple modules, as described here

Categories