smwikipedia
smwikipedia

Reputation: 64205

How to generate Ant and Maven build files for an Eclipse Java project?

I have a project which is released like this:

Now I am using some static code analysis tool which requires the project to be built with ant or maven.

It seems both Ant and Maven relies on some build file, so how can I generate it from my Eclipse project?

I tried Eclipse Export -> Ant Buildfile, but the exported buildfile seems still relies on Eclipse built-in builder.

Maybe I am wrong but my understanding about a builder server should comprise:

Whenever a new source is checked in, Ant can be launched to build the source with JDK. No eclipse should be involved.

I am new to this. Please shed some light.

Upvotes: 2

Views: 5350

Answers (3)

M A
M A

Reputation: 72854

The simplest solution is to export to an Ant buildfile in Eclipse by right-clicking on the project and select Export --> Ant Buildfiles.

This would create a buildfile which contains a default target to build the project, in addition to targets which can be used to run your programs. For example, if you already defined run configurations in Eclipse to run a main method, a target would be created that allows you to run the program from outside Eclipse. If you have defined a JUnit run configuration to run a certain test, the export would also generate a target to run that test from outside.

Converting to Maven requires more work -- AFAIK there is no official tool or wizard that allows you to generate a full POM configuration from an existing Eclipse project. You can mostly convert to a Maven project by right-clicking on the project and selecting Configure --> Convert to Maven Project but this will only generate a POM file and allow choosing the Maven coordinates. Dependencies and plugin configurations need to be added manually.

If you still need to add dependencies into your POM automatically based on the project's .classpath, I found this project (haven't tried it though) which seems to do the job.

Upvotes: 3

E-Riz
E-Riz

Reputation: 32914

Changing a project to Gradle or Maven (I prefer Gradle, which is like the best of Maven and Ant, combined) is a "big" step (really more of a "medium" step), but probably will serve you well in the long run as a strategic choice. Having said that, if you really want just a tactical option to quickly generate an Ant script, you're on the right track with Eclipse's Export wizard.

Export > Ant Buildfiles is the starting point, just make sure to uncheck the "Create target to compile project using Eclipse compiler" option in that wizard. The generated Ant file should be free of any Eclipse requirements, meaning it should be runnable from a command line without Eclipse.

Upvotes: 3

Iker Aguayo
Iker Aguayo

Reputation: 4115

If you want to convert a project to Maven, you can do right click on the project and follow the picture.

And you get the pom.xml (the configuration file in a Maven project).

And in the pom.xml you have to configure the dependencies you need in the project and the plugins to achieve what to want to do. For example, to create an .exe file with your projects, you could see my answer in this thread

Maven

Upvotes: 2

Related Questions