Where can I find updated and "unbloated" webapp archetypes for maven? - maven

I'm pretty new to maven and I'm trying to find a good archetype for creating a webapp "starting point" for a Spring MVC project. I have been rtfm-ing, googling and installed many of the archetypes I've found in lists presented here on SO and other places, but I never found one fitting my needs.
Example 1: I installed org.apache.maven.archetypes -
maven-archetype-webapp, but it seems outdated, as it uses JRE 1.5,
JUnit 3.8.1 and also it's missing src/test/java, src/test/resources
and src/main/resources by default.
Example 2: Using eclipse, I created a new "Spring MVC Project" from "Spring Template
Project", which also set up Maven for me, but it installs loads of
stuff I'm not sure I even need.
I also tried some other archetypes, like from AppFuse and Cocoon, but the "bloating" in their basic webapp archetypes was even worse.
What I'm after is kind of what the maven-archetype-webapp is giving me, but an "updated" version that also includes the test and resource directories.
So I guess my question is two folded:
1. Is there an "updated" version of the maven-archetype-webapp archetype?
...and/or...
2. Is there a way for me, as a maven newbie, to create my own archetype that fits my needs?
... or have I totally been misunderstanding the concept of archetypes?

Is there an "updated" version of the maven-archetype-webapp archetype?
AFAIK, version 1.0 is the last version (and is not very up-to-date). BTW, it seems it doesn't define the source code level (so I guess 1.5 you have is coming from your IDE)
Is there a way for me, as a maven newbie, to create my own archetype that fits my needs?
Creating a new artifact is quite simple. Here are the details.
Is it so simple for a maven newbie ? Well, it depends... but according the quality of your post, it seems you understood the basics so yes, it should be easy to do.
Basically an archetype is nothing more than:
the target project structure you need (with some files like pom.xml, web.xml, ...)
a file describing the resources that need to be included in this structure.
Take a look at maven-archetype-webapp-1.0.jar (i.e. unzip it) and you will immediately understand why the directories are missing and from where junit-3.8.1 comes from.
Note: you will find the maven-archetype-webapp-1.0.jar in your maven repository. (Probably here : /.m2/repository/org/apache/maven/artifacts/maven-archetype-webapp/1.0/... but I didn't check)

Related

The idea behind using maven to compile source code

I am currently starting my adventure with Maven, and I actually don't understand the idea behind using it to automate compilation of my source code. For the time being I am working on small projects with up to 15-20 classes, and 1 main method in the "app" class. Could someone please give me the explanation with examples, when it's necesarry (or recommended) to use build automatation tool to compile the source code and how could I benefit from using it regarding source code compilation?
Thank you very much in advance!
I was looking for different answers and I have a lot of work to do but since I've seen this question, as a Maven fanboy, I couldn't resist anymore and this below is my answer.
First of all, I agree with JF Meier which answered before me, but I think the answer can be improved.
IMO you have to consider Maven not just as a build tool, but as a multi-purpose tool which can help you to do very different things. The best 3, for me are:
Compiler. Obviously. Maven allows you to easily compile giant projects with a lot of submodules, even if some of these modules are interdependent one with each other.
Dependency and repository manager. Maven allows you to automatically download third party software and bind this downlaod to the build. This is immediately understandable if you think to framework or api dependencies from big corps (Apache found., Spark, Spring, Hibernate and so on ...) but it's really powerful in every enterprise context.
Example: you have a Maven project (let's say project A) which manages requests coming from a webservice and provides responses. This Maven project relys on another Maven project (let's say project B) which actually generates webservice jar and uploads it to a company repository. Well, when you have to add a field or a method to the webservice you just have to implements new software in project B, upload it the repo and change the version in Maven poms in both project A and B. Voilà: now EVERY developer of the company just have to "mvn clean install" project A to have the new version.
Sources and code automatic generator. Since Maven 2.x are available a lot of plugins (from Apache found. and others) which allow you to generate code and sources (tipically xml files) starting from little to none implementations.
Example 1: CXF plugin is commonly used to generate java classes from xml or xsd files.
Example 2: JAXWS plugin is commonly used to generate wsdl from SOAP webservice implementations or implementation starting from wsdl file.
Do you feel the power now?
-Andrea
The question is not very specific, but I will try to answer.
Usually, you want your source code to end up in a jar or war, so that you can use it as a library or run it somewhere (e.g. on an application server).
Maven not only compiles the classes you have and creates the final artifact (jar, war), but also handles your dependencies, e.g. the libraries your project depends upon.

Is Maven a framework that provides mainly an archetype like domain.controller-view in grails?

I´ve been reading lot about, but since there are several web frameworks that uses Maven for the project, I got confused, so I´m not entirely sure if Maven is an archetype that defines an schema to start developing apps by following good practices, or is just some piece of sdk that converts my code to bytecode. Thanks in advance to anyone who can drag me out of my confusion and gave me the required info. BTW is that rigth to say an archetype is a directory structure?
I am not sure if you are reading enough about maven, Maven is a build system which can help you build your application, manage your dependencies, run your tests, create reports and many other things.
First link in google result is http://maven.apache.org/
Apache Maven is a software project management and comprehension tool.
Based on the concept of a project object model (POM), Maven can manage
a project's build, reporting and documentation from a central piece of
information.
Each application has many dependencies and many small tasks that needs to be done before you can run your application, developers define them in a file called POM and that will be a instruction for Maven to build the application. Maven can do pretty much everything other than writing your code. In that sense it is like Genie in the story of Aladdin, you wish for something it will bring it for you.
There is a Grails maven plugin that can populate Grails project with the same convention that Grails uses. It can work with Grails to execute your commands and many other. More importantly it will manage your dependencies.

How to easily copy/rename/remove files with Maven (as in Ant)

I am working on a project and using Maven to build it. The project is a quite big Java web application and it is supposed to work with both Mysql and Oracle databases.
The problem is that there are some specific annotations related to either of the two databases in the source code, plus some other differences, so that I am forced to manually comment/uncomment part of the code before building the application for one of the two databases.
Basically what I would like to achieve is to have my build script, maybe via a Maven profile, to automatically switch the source classes before building depending on the database I want my war to work against.
Putting it simply, the idea is to have MyClass.oracle and MyClass.mysql, and depending on my build profile I should move one of the two in the source dir, rename it MyClass and build. This should be done for some packages, classes, and also configuration files.
Is there any way I can achieve it via "pure" Maven? The only solution I came across till now is to use an antrun plugin and reference an Ant build.xml inside of it.
Thank you,
Mattia
A pure maven solution would be to develop your own maven plugin. Depending on your requirements this can be an overkill, however it is not hard at all, you can see how to achieve this here.
This is a limitation of Maven. One of Maven's purposes is to not have a build script. You should simply use the plugins as available, and setup your project the right way, and magically, everything will build!
There is one solution: Use Ant. Well, not to redo your whole project with Ant, but with the antrun plugin, you can run a few Ant tasks at various phases of your Maven build life cycle.
It's been a long, long time since I've used this, so I am not going to try to write a test pom.xml, but I don't remember it being very difficult to use.
Of course, the correct Maven solution is to divide your project up into "common core" code, and then a separate Oracle and MySql client that uses the "common core". By the way, I hope you're not patching source code. Instead, you're using a properties file to do this for you.

Migration from existing Java EE project to maven without changing directory layout

I have seen many questions concerning this issue, but I'm new to this topic and I have to convert previously written project (standard, Eclipse Java EE project) into Maven.
My first problem is that I'm completely new to Maven. Second - I can't change the directory layout. And third - I'm running out of time.
Some of topics I've read covered few of my questions, but I need it clearer.
I have 3 modules connected with each other:
* Main one with catalogues: .settings, db-schema, EarContent,
* EJB with .settings, bin, build, ejbModule
* WEB with .settings, build, src, WebContent.
As far as I know (correct me if I'm wrong) there is just one 'mapping' when I have single catalogue with source code and it looks like this:
<build>
<sourceDirectory>ejbModule</sourceDirectory>
...
But what if I have two catalogues? I mean the WEB module - src and WebContent.
What else do I need to change if don't use standard directory layout?
What with the main module with EarContent? - There is no source code, but there is an -ds.xml file which (I guess) I have to put somewhere.
In main POM.xml I know that I have to add tags, but is there anything else I should do?
Sorry for (maybe) not precise and wide questions, but I've started today and I have to do it fast. So far I've found Sonatype site and managed to get one of their pdfs, but I'm reading it quite too slow and need 'some' help doing it. I've also downloaded the archetype for nexus repository and I'm trying to understand it.
Try to use add-source goal of build-helper-maven-plugin.

Should I use POM first or MANIFEST first when developing OSGi application with Maven?

There are two main approaches when developing an OSGi application with Maven: POM-first and MANIFEST first.
I'm looking for an answer that is in a form of a table that shows pros and cons of each method.
To be more specific, I would also like to know how it relates to:
Maturity of toolset
Vendor independence
Development ease (which includes finding people who can do the development on the tooling)
Compatibility
Avoiding ClassNotFound
Avoiding manual work
At present this is what I can come up with
POM-First Pros (using maven-bundle-plugin)
Leverages existing Maven skills, repositories and tooling.
Likely easier to find people who know how to manage pom.xml rather than MANIFEST.MF along with pom.xml
Most of the information in MANIFEST.MF can be obtained from the pom.xml itself.
Can work with other IDEs not just Eclipse based ones.
Less invasive, just add the single plugin and change the packaging type to "bundle"
POM-First Cons
ClassNotFoundException more likely to occur at runtime. However, this can be mitigated using pax-exam (although it is very complicated to set up).
Still need to understand how the MANIFEST is setup to make sure the instructions configuration element is set correctly.
MANIFEST-first Pros (using tycho-maven-plugin)
Seems to be the recommended approach, or at least talked about as the recommended approach, but I can't really see why it has significant benefit. (Hence why this question was asked).
Good for developing Eclipse plugins and integrates well with PDE
Provides tooling for testing thus allowing ClassNotFoundException to appear during JUnit testing rather than runtime.
MANIFEST-first Cons
Seems to only work well on Eclipse based IDEs. You don't have to use Eclipse, but without the PDE would you want to?
Violates DRY principles since I have to do put keep the names and versions from the POM and MANIFEST.MF in sync.
Need to name things in a specific fashion
You cannot mix, meaning existing Maven multi-project installations cannot just tack on OSGi support
A lot more configuration compared to maven-bundle-plugin is needed to get less warnings: http://wiki.eclipse.org/Tycho/Reference_Card#Examplary_parent_POM
Have to make test cases a separate project. It won't run when built in src/test/java.
Seems that it will only test classes that are exposed, in other words those in ".internal." is not testable.
If I were asked for a recommendation for an enterprise that is using Maven already and want to move to OSGi then it would be POM first
If I were asked for a recommendation for someone who is doing Eclipse plugin development, then it is Manifest first -- with tycho
I think you should choose by use case. For server side OSGi projects I favour the pom first style. It nicely matches the maven builds and is much less error prone than Manifest first.
In fact bnd which is behind the maven bundle plugin gets the Manifest right for most cases without any additional config. The trick is to use some naming rules. For example if you name internal package impl or internal the will not be exported. Using this style you can not use the Eclipse plugin perspective (at least without bndtools which I do not like) but I did not yet miss this perspective. I am a developer in the Apache Karaf, CXF and Camel projects where we use this style and it works great. Especially for CXF and Camel it is great that we can support OSGi and non OSGi deployments with the same build and tools.
For Eclipse RCP applications Manifest first is the way to go as you need the plugin perspective and the Eclipse IDE tools. If you want to combine that with maven then tycho is probably the way to go.
MANIFEST first does not lock you to Eclipse (although I'd be surprised if more than a tiny minority would use anything else). The MANIFEST is the file that counts, and needs to be added to a jar, regardless how you do that.
On the other hand, POM first completely locks you to Maven, you lose the advantage that an OSGi bundle is a regular jar you can make any way you want.
I've tried both, I really prefer MANIFEST first. The MANIFEST file is a really important file, I prefer to craft that file over crafting a file that produces that file. If something weird happens, (and it will at some point) the MANIFEST file is the first to check, it's just easier if it's your own file. Besides, you will have to be familiar with it anyway.
So, if Maven is your alpha and omega, POM first will suit you best, but you'll still need to have in-depth understanding of the MANIFEST file.

Resources