MySQL, Eclipse, and Java - is Ant required? - java

I am starting my first project with MySQL and Java in Eclipse. I found a tutorial:
http://download.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html
It is full of ant builds and XML files which make me want to explode. Do I really need to use ant builds or can I just import the MySQL driver into my project and import the libraries into the class and away I go? MySQL is already setup.

If you are a beginner and just want to learn some Java and MySQL, then Ant is not required. If you are building a project for some class or project. Then Ant is something that you should use. Ant automates your manual steps like compiling and packaging which will become a overhead over a period of time. Ant just manages your build process.
If you are not into build and stuff then happily skip ant. Just add the mysql connector Jar to your classpath and you are good to go.

You don't need Ant for development, Eclipse can build your projects just fine.
Once (if) you get into build automation, take a look at Ant again. Before that, you can safely ignore it (as long as you still know how to follow the steps in your tutorial).

Ant is not required for Development, with help of eclipse can build your projects.

Related

When would I use ant for Java web development?

I do not understand the purpose of ant. I read the introduction, but am missing the big picture. I know the following (or at least that I want to use them),
Java
jdbc
hibernate
jsp
servlet container like Tomcat
But I don't see how ant fits in.
Is there some all comprehensive tutorial that tells me how to put all this together?
Why would I want ant? What is the alternative if I don't have ant?
EDIT: So is this sort of the compiling part of its Visual Studio counterpart?
Ant is used as a build system. You do not have to use it--in fact there are quite a few better systems but since ant was one of the first it's still likely to be very common. It generally is used to compile your java files, create wars and often even put the .war files onto the tomcats.
Maven is bigger and tries to do a lot more, but it's also rather strict in it's format and quite heavy--but it's quite common these days as well. Maven can gather all your dependencies automatically which is nice, with ant you still have to download everything yourself.
There are others, but you get the idea.
Response to comment:
I don't use visual studio, but I think it is more limited. For instance, Eclipse can build all your java files into a jar or execute them directly (in fact, it uses ant internally). If you wanted to take your visual stuido "Build configuration" and put it into another tool (Perhaps an automated build system like Jenkins) that doesn't rely on visual studio directly, can you do so?
I guess Ant is the way we code our builds--it's portable betweeen many tools. Visual Studio would be cleaner to "create" your build, but "Exporting" the ability to build would be more difficult (if even possible).
Also ant is portable across operating systems as well as tools. We can switch from Eclipse to Netbeans to IntelliJ without touching our build toolchain.
How would you completely remove visual studio and replace it with slickedit, emacs or vi (if that was your thing, which I'm sure it's not) without starting over from scratch with your build?
For any web application to work you need to package or build all your java code with all the libraries into an web application archive to accomplish this task we need to some tool or library to package into the correct format and some of the libraries which can achieve this is
Apache Ant and Apache Maven
There some good discussions about Ant vs Maven in SO.
You need ant to build the project. Well you can build project using IDE like eclipse etc. But for production purpose and big projects, it always advisable to some build script like ant. Its a script where you can customize what you want to build and what not. you can define what is the output should be like war/jar/ear etc
Other alternative is Maven which is very prevalent and standard now a days

How can I open non-Netbeans Java project using Netbeans?

I'm using Netbeans for my Java development, and every time I download a project that has been developed using other IDE, I can't run the project because of errors.
Is there any way to open regular people's projects easily without headache.
Note : the project am trying to open is not an Eclipse project, so I can't use the Eclipse importer.
And usually what are Java developers using for development?
Most Java developers are split between Eclipse , IntelliJ Idea and NetBeans. NetBeans is capable of opening Eclipse projects and Idea can export to Eclipse. So this should solve most problems.
Another way is to check if you project is using Maven or a similar model. Generally there are plugins for Netbeans that can handle import from this sort of sources.
I've had some luck using File > New Project… > Java > Java Project with Existing Sources, which "Imports an existing Java application into a standard IDE project. The Java application can have multiple source folders. Standard projects use an IDE-generated Ant build script to build, run, and debug your project." Conveniently, the generated script includes targets that can be overridden to alter the build process, as seen here.
We have several projects with existing ant build scripts and all I want is to have NetBeans as my code editor ...so far.
We aim for that the projects are independent of IDE as we have been used to just use emacs and ant for coding/building, which means that independant on platform you should be able to checkout the code and just build it no matter if it is within an IDE or just a shell/"DOS prompt". If you have projects like that use the "Java Free-Form project" and add all jars that you have defined in build script to also be included in Netbeans CLASSPATH.
This type of project will use your ant scripts for everything and will still work even if someone in your project prefer to use other coding environment.
Hope this can help

Java equivalent to VS solution file

I'm a C# guy trying to learn Java. I understand the syntax and the basic architecture of the Java platform, and have no problem doing smaller projects myself, but I'd really like to be able to download some open source projects to learn from the work of others. However, I'm running into a stumbling block that I can't seem to find any information on.
When I download an open source .NET project, I can open the .sln file with visual studio and everything just loads. Sure, there's occasionally a missing reference or something, but there's really very little configuration required to get things going. I'm not sensing the same ease of use with Java. I'm using eclipse at the moment, and it feels like for every project I have to create a brand new Eclipse project using "create from existing source", and almost nothing compiles properly without significant reconfiguration. In the case of web projects, it's even worse, because Eclipse doesn't appear to support creating a web project from existing source. I have to create a standard Java project from source, then then apparently modify the project file to include the bindings for the web toolkit stuff to work properly.
Assuming I want to be able to contribute to a project later on, I shouldn't have to be making such drastic changes to the file structure to get my IDE to a workable state. What am I missing?
The best way to go about this, is to first remove the IDE from the equation. In C# there is only one environment, so the presence of the default IDE is assumed. In Java a default IDE does not exist.
In the end Java is all about java source files and supporting jars. If you figure out what those are, your 99% of the way home. Then you can apply you favorite build system for the set. Some project require a runtime environment, like a webserver to handle the JSP files. If you understand what the basic setup is (as specified by the specification) you can quickly setup your IDE to handle that.
If I get a project with java files and supporting jars, I fire up Eclipse, create a new project, point it to the project's base directory and Eclipse will automatically detect what it finds and set up the project accordingly.
But projects often come with a build environment included. The trick is to figure out which one:
if a build.xml file is present, it is using ANT. This is a "make" like tool. You can execute "ant" in the directory where the build file is (if you have ANT installed) and it will try to compile. All IDE's like Eclipse and NetBeans recognize the build.xml file and allow for starting ant from inside the IDE. There is no guarantee the supporting jars will be present.
if a pom.xml file is present, it is using Maven. Maven is also a make like tool, but enforces a much stricter build cycle. Plus (and this probably is its biggest advantage) it automatically downloads supporting jars. If you have Maven installed you will be amazed at what it downloads... just sit tight, it'll work out in the end. IDE's usually require a plugin to support pom.xml, but then you automatically have the whole project setup at once.
if a .project file is present, it usually is a Eclipse project
if a nbproject directory is present, it is a NetBeans project
Getting to know a build environment / IDE is more work that trying to setup a project in the one you know. So I always try to get it running in Eclipse. Usually projects are quite simple to get running once you know your IDE.
Having multiple ways of doing things is not always pleasant, but it's the cost of having an open community. If there is only one IDE it makes things easier, but I like the fact that there are more people trying to figure out what the best way is to get things done.
In some cases you really may have to make drastic changes. A well-designed build system will require no configuration at all on most platforms and perhaps a few changes on exotic platforms. However, there is no single standard build system for Java; some people use Eclipse, some people use Apache Ant, and others use Apache Maven or Apache Maven2. If you were to create a project from scratch, then Maven or Ant is probably the ideal way to go. If you use the NetBeans IDE, projects that you create will automatically contain an Ant build file (so that it can be built on all systems using Ant), but will add additional metadata so that it is recognized by NetBeans IDE. If you create a Maven project, either using Maven directly or using an IDE such as Eclipse or NetBeans, then that same project can be loaded in either NetBeans or Eclipse without any additional configuration changes (although you may need to install a plugin for Eclipse for it to recognize Maven projects; NetBeans recognizes Maven projects out of the box). If you are starting a project from scratch, you may be interested in the Java Project Template. If you are contributing to an existing project, how you view/edit the project depends on the build system chosen; if the project already uses Maven or Ant, loading it with other IDEs should be fairly simple, while if the project uses a specific IDE's quirks or uses some more exotic build system, it may be harder.

How do I create an ant builder file (build.xml) for an existing Java project?

I am working on an Java assignment for a software design course in my university. It's not really complicated and it includes some classes, interfaces and jUnit test cases. We were now told we should supply a build.xml file as an input for an ant builder. I have never heard of, or used ant before. I also saw Eclipse supports it.
My questions are - What does build.xml do? How does Eclipse builds my project and why not do the same instead of using ant? And most important - how to create this file with Eclipse?
Thanks.
Ant is a build tool based on Java. It is very powerful, and integrates with various other tools you might need to build any imaginable Java application. It is very useful when deploying a Continunous Integration environment with a Continuous Build server.
You are building within Eclipse, and your build process is bound to your IDE - which is perfectly fine for a one-man project but might prove unefficient for a team project. Also, with Ant, you can have a complex build including multiple components and intricate dependencies done in a single step, including unit tests.
You can either create your Ant build from scratch (which is the best way, but it takes quite a while). Otherwise, you can create it with Eclipse using the File/Export menu (under General, select Ant buildfiles).
You should definitely know how to crate a build.xml file from scratch, just so you know what Eclipse does behind the scenes.
I taught a Java class a while ago where I explained the basics of ant.

Automation of EAR packing

I need to automate packing of Java EE projects into EAR archive in Eclipse. The most easy way, I think, is using Ant script. But writing of Ant from start is routine and long task. Is there a way to generate Ant script based on my actions in eclipse (like macros in MS Office)?
Or, maybe, exists easier way to do this?
Thanks!
There is nothing I know of that will generate an Ant build file for you. I don't think it is that difficult or long to create an Ant build file. If you have never done it before there is obviously a learning curve to it.
What I think you might be asking is can you have a build file that uses the same build configuration as your Eclipse project so that you don't have to setup your classpaths yourself then the answer is yes. I have never done this myself but I see this link seems to be offering that:
ant4eclipse
Just another thought... You can use Maven instead of Ant. using Maven you can specify packaging type and you don't need to write ant script. This will also lead to simplified build management of your app application.
But writing of Ant from start is
routine and long task.
Ridiculous. It's not a long task. And yes, it is routine. Do it once and forget it.
Laziness is a good trait for a programmer, but this is going too far.

Categories