ScalaTest in Java Eclipse project - java

I'm fairly new with ScalaTest, and now that I've got it running with Maven, of course I'd like to have it working well in Eclipse as well. My project is a Java project, but I want to improve my Scala skills by writing the tests with ScalaTest.
I understood it so that I should right-click on my project, say "Configure" and "Add Scala Nature". Doing that, however, makes Eclipse try to compile all my Java files with scalac, giving me a lot of "Scala Problem" entries in the problem list. Of course, not having the Scala nature gives me a lot of "Java Problem" entries in my project for all of my Scala files. How can I add the Scala nature only to src/test/scala?
Cheers
Nik

Maybe the simplest solution (in your context, i.e. classic Java project, without M2Eclipse and a Maven project) would be to have two separate projects:
one with only the Java Nature
one with the scala nature for tst.
Since you can link a directory in your second project, you don't have to move the sources of the tests(src/test/scala) from your existing file set.
You only have to exclude src/test/scala from any compilation in the first (Java only) project.

Related

Is it possible to load an eclipse Java project using JDT in the headless mode?

I have an eclipse Java project and want to get information like project source dir, classpaths, etc. My current implementation parses the .project file. But as I didn't find any official documentation describing the structure of the .project file, I have some concerns for the robustness of this approach.
A more convenient and robust way would be to use JDT (headlessly) to load the project and get the relevant information from the IJavaProject object.
Although the developer guide of JDT says
JDT Core packages give you access to the Java model objects and headless Java IDE infrastructure.
all the examples I can find opening an existing Java project get the IJavaProject object from projects within a workspace or use an IProject object. But I couldn't find the way to add a project to the workspace or to construct a IProject/IJavaProject from a path to .project file.
Could anyone please help?
Yes, and can all be done through clear and stable API. org.eclipse.jdt.core is a plug-in, just like org.eclipse.core.resources (which is where you would get an IWorkspace instance), and they both expect to be running within an Eclipse runtime, which can be headless if that's how you write your Eclipse Application. JDT uses the .classpath file to record where sources, libraries, and build output are, and what abstracted references to libraries to use, while the .project file is what records what kind of project it is in general--Java, PHP, Web, some combination of those or others--and a little more information about what builders to execute.
So make yourself a headless Eclipse Application, or package your end-goal functionality inside of one.
https://wiki.eclipse.org/FAQ_What_is_an_Eclipse_application%3F
https://wiki.eclipse.org/FAQ_What_are_extensions_and_extension_points%3F
http://help.eclipse.org/mars/topic/org.eclipse.platform.doc.isv/reference/extension-points/org_eclipse_core_runtime_applications.html?cp=2_1_1_27
http://help.eclipse.org/mars/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/ResourcesPlugin.html#getWorkspace--
http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/JavaCore.html#getJavaCore--
http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/IJavaProject.html#getResolvedClasspath-boolean-
See http://www.ant4eclipse.org/ for a project which allows you to work inside Eclipse projects.
I used it for a bit some years back, and found that this is too brittle for long term usage and build scripts. I would recommend against relying on internal Eclipse things. They break! Typically when mixing releases.
Instead I would suggest you move to Maven. This is a bit more work, but allows you to use the whole maven infrastructure and API's to do things. What you want to achieve here, may be possible already using a Maven plugin.

How is a Java Project Structured (Compared to a Visual Studio C# Project)?

I'm trying to learn project automation and I'm using the book Pragmatic Project Automation as a guide. It's examples are in Java so I find it easier to follow it along in Java. I don't have any experience using Java or any of its IDEs. However, I've learned some C# using Visual Studio (although I'm still a beginner).
I'm having trouble understanding some parts of setting up a Java project. I'm using Netbeans IDE 7.0 on Windows 7. With Visual Studio, I have a solution with projects underneath. With Netbeans, I appear to just have a project with directories determining the rest of the structure (and the IDE to some degree)? The equivalent to Add Reference appears to be adding a source to the Classpath. There also seems to be a degree of separation between the compiler and the IDE. I'm currently in a situation where I can compile my project just fine while the IDE tells me I still have errors (and I assume this is because I have the project set up incorrectly).
I'm basically looking for analogies that will help me better understand Java project structure.
Lots of similarities between the two languages and IDEs. I spent many years in both. For starters the equivalent to 'add reference' in VS is adding a library or jar in netbeans. In respect to a reference - a jar is pretty much the same thing as a module or .dll in VS. A jar is a compiled reference. To add a reference just go to the project menu and then properties then to the libraries menu from there you can add either pre-assembled netbeans libraries, which are collections of .jar's, or a single .jar, or even a project. Once you add a reference you can import them into your class just like you would in C#.
Netbeans doesn't really have a 'solution' as VS does. You deal with individual projects. It does however have the capability to add a project as a reference so you don't have to continually re-build the references when you change something between multiple projects. It also has project groups to group similar projects.
Lastly Apache ANT is responsible for tying everything together in the background. Netbeans creates a build.xml and build-impl.xml file in the background to tell ANT how to assemble the project into a .jar.
There are my other things to cover but I thing this answers most of your questions. Does this help?
I can't speak for NetBeans, as I use Eclipse, but you are on the right track with classpath being roughly equivalent to references in the Visual Studio world. Libraries (usually .jar files) are placed on the classpath and must be there both at compile time and runtime (you specify the classpath to the compiler at compile time, and to the JVM at runtime). The classpath can contain many different entries, and they can be anywhere in the project structure (or outside of it entirely).
Java itself doesn't impose many restrictions on your project structure, although various IDEs and build tools do. The one thing that is a universal restriction in all Java environments is that source files (and class files) are placed in a directory named after the package name. So if your package name is com.test.something, then your source files will be in SRC_DIR/com/test/something, and your class files in OUT_DIR/com/test/something (note: SRC_DIR and OUT_DIR are not special variables; each IDE will have a different way to specify those directories).
Java libraires tend to heavily build-on one-another, so at some point, you'll find that the classpath entries are too many to manage manually. Before you get there, you'll want to take a look at Apache Maven or Apache Ivy which are dependency management tools. You'll need to understand how they work (either one, not both) and how to integrate them with your IDE. If you use Eclipse and Maven, m2eclipse offers fairly complete integration between the IDE and the dependency management tool.
With Netbeans, I appear to just have a
project with directories determining
the rest of the structure (and the IDE
to some degree)?
Visual Studio dictates a particular project layout and since the compiler is so tightly integrated into the IDE there's no real concept of a build script. In contrast, Java has no such structure (although certain 'best practices' have emerged such as having a 'src' directory for source files, 'lib' for libraries, 'test' for test source, etc.) and a build script is usually required to tell the compiler were to find source files and libraries, what artefacts to produce and a miscellany of other chores (running tests, deployment, creating code metrics and so forth).
In simple cases, the IDE will take care of this for you (if you follow the convention for that particular IDE) but ultimately you will probably want to take a look at a build tool to understand what's going on behind the scenes. Apache Ant and Apache Maven are both prominent offerings. Ant is very flexible whereas Maven attempts to dictate a common layout. I suggest you investigate both and see which suits.
There also seems to be a degree of
separation between the compiler and
the IDE. I'm currently in a situation
where I can compile my project just
fine while the IDE tells me I still
have errors
If your code compiles, it is correct. The IDE is simply acting in an advisory capacity (and will highlight issues beyond compiler errors, such as warning you of potential code mistakes or bad practice).
and I assume this is because I have
the project set up incorrectly
This is a possibility although, as stated above, there are many other explanations.

Verifying Java Class Library in Netbeans 6.9.1

I am new to java and to netbeans so my apologies if this is a simple question.
I made a 'Java Class Library' Project in Netbeans 6.9.1
I added a few classes to it and hit 'Build'. It builds with no errors. However the problem is I know there are errors.
It seems as though I can make up class names and hit build and it doesnt provide me with any feedback.
How can I make it so that netbeans validates my code when buildling a class library?
I am unable to 'run' the project becuase there is no Main. However this is a class library and I dont want to make test applications in my library.
Edit:
For example I can write the following and a 'clean & build' still works
MadeUpName x = new MadeUpName();
This will build even though I have no class or reference to a class that contains
MadeUpName
I also have no 'Import' statments as of yet and it still builds....
Thanks, stephanie
1) To test a Java Class Library project you should create another project and add your "Java Class Library" project as project dependency. In this new testing project you can write class with main method to test features of the class library. When you will build the test project NetBeans IDE will also build the class library project and will add the JAR file in the class path of the testing project.
2) If the Java Class Library projects compiles through "Clean & Build" that means the project has no syntax errors. That means your code may have logical errors which are not detected by the Java compiler. Usually class library developers create JUnit test cases to find out logical errors, and you are also strongly advised to use JUnit tests for your project.
with regards
Tushar Joshi, Nagpur
Ideally you should test a class library by writing unit tests (rather than test applications) that exercise the classes in your library. There are several unit-testing frameworks available that can help you write the unit tests. The most popular is JUnit.
The test cases should be placed in a different source folder in the same project as the code they are testing. When you build the project you should ensure that the test classes are not included in the JAR file. If you use a build tool like Maven it will do this for you as long as you follow it's project conventions.
Update
Based on your comments, and the fact that you tagged your question with Groovy, I'm guessing that the library is written in Groovy? Your problem seems to be that Netbeans doesn't perform the kind of type-checking that you get with Java (or other statically-typed languages)?
Because Groovy is a dynamic language, it's not possible for the compiler to perform the same rigorous type-checking that you get with Java, but at the very least the Netbeans Groovy editor should provide some hints/warnings if you're referencing classes that don't exist (for example). Are you sure you're opening the code in the correct editor (you may need to install a Groovy plugin first).
You should use GroovyTestCase, rather than JUnit directly to test a Groovy library.

How to see full compile path in Eclipse?

I have looked for an answer for this nearly every where that I can think of, but there doesn't seem to be any way to actually SEE what Eclipse "runs" to compile the projects (as it does need the JDK installed and visible to actually build). I ask because I imported a few jars into my project, and even though I've looked through all the javac documentation, I can't seem to figure out how to mimic it quite like Eclipse does. I really, really need to be able to compile on the command line in this case - Eclipse or any other IDE just isn't what is needed.
I started to look through the Eclipse source, and although this sounds lazy, I just became overwhelmed and figured I would ask here first, hoping someone else had the same question at one point.
Eclipse JDT does not require the JDK and does not use javac - it uses it's own compiler.
You can see the classpath by reading your project .classpath file. The various builders that are used to perform build operations (Java, or whatever the project builds) are listed in the .project file. (These are also listed in the project settings.)
It is possible to invoke Eclipse to build your project in headless mode, or write Ant scripts that can be executed both with the JDK and within Eclipse, or install Maven support for internal and external building. It is also possible to configure the project builders to rely only on external tools.
Look at these two articles.
http://www.eclipse.org/articles/Article-Builders/builders.html
http://www.eclipsepluginsite.com/builders-natures-markers.html
Look at your .classpath file and start building an ANT build.xml. You need to do this to be able to have consistent builds on a build machine anyways. It is unlikely that a build server would have eclipse installed on it anyways.
Maven is also another tool that is used for builds. In our shop we use Ant.
Have a look at ant4eclipse - this project allows for generating the appropriate ant data structures for invoking <javac> from the .classpath files and a projectSet.psf file.
By using this we can use Eclipse "natively" and bend ant to conform to Eclipse. The usual approach is the other way around.

Groovy Eclipse Stubbed Method

I have some groovy files in Eclipse and my project has the Groovy nature. I don't use the GroovyBuilder (I don't think you need to now. there were problems in the past when java files referenced groovy and vice versa). Sometimes when I run my project I get a "Stubbed Method" error and I need to do a project clean for the groovy files to rebuild. Any ideas why I need to do this and what causes the files to need rebuilding if I'm not changing them?
thanks,
Jeff
It sounds like you are using an old version of the plugin. Please upgrade to V2 of groovy-eclipse (still in alpha, but quite stable).
The update site is available here:
http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.5/
Also, be sure to convert all your old groovy projects to the new format (see the groovy preferences page).

Categories

Resources