Is it possible to use a maven archetype to prompt for an input during archetype:generate? - maven

I'm exploring the use of maven archetypes to generate a starter project. What I'd like to do is prompt the user for input, and then do something with that answer. I know it's possible to prompt for additional properties using archetype.properties. Is it possible to take the input from these properties and do custom processing of the project structure during project generation?
Specifically what I want to do is include or exclude sample code from a starter project. Some people will want to see a working example and then delete once they're ready to start, while others just want to get started with a blank project.

There are some open issues against the Maven Archetype plugin which (I think) relate to your use case:
https://issues.apache.org/jira/browse/ARCHETYPE-424
https://issues.apache.org/jira/browse/ARCHETYPE-274
These have been open (and largely inactive) for a long time, so they don't appear to be likely candidates for completion.
In the absence of built-in support for your use case and assuming there are just two possible outcomes (1. exclude sample code, 2. include sample code) then perhaps you coud provide two archetypes?
Alternatively, you could provide a single archetype and include a link to sample code (hosted elsewhere, perhaps on GitHub if the sample code is to be freely accesible) in a readme in the root of the generated project.

Related

Maven Archetype creation via Eclipse

I am willing to create an archetype which will be useful to create a lot of projects sharing the same architecture. I followed the maven guide and read some bloggers guides but even with these informations I am struggling.
I have some constraints for my projects :
All the project code is in a package which must be something like com.ei.app.project (app and project are vars I would like to set during the project generation)
I have dependencies I would like to be present in my POM when the project is generated
I have some scripts file with data (paths, app name...) and I would like to set them according to the project parameters, and if it is possible add additional parameters.
We don't use the official maven repo but set up another online repo. We have a strict security policy an whenever we have to add a lib into the repo it takes a lot of time for verifications.
How can I achieve this ? I tried several solutions but encountered a lot of errors. If you have a complete step-by-step guide in english or french that would be fantastic, but I would also like to understand how it works.
Many thanks for your help
Let me answer parts of your question.
From an example project, you can create an archetype by using archetype:create-from-project (https://maven.apache.org/archetype/maven-archetype-plugin/create-from-project-mojo.html).
You can supply a property file that will be used to replace actual values (like 1.0.0-SNAPSHOT) with references (like $version). All filtered files will be treated as velocity templates so that you can use $-type replacement.
Your repository restriction may be problem because you probably need dozens of artifacts to make the archetype plugin run.
I finally created a "base" project with all I need inside (libraries, main Vert.x class...), and developed a script to replace a template value (i.e __projectName__ ) in dir / file names and inside configuration / pom.
It's close enough to what we need and provide a cool replacement tool for my company, which will be used for multiple purposes.

Maven plugin to test i18n properties

does anybody know a maven plugin that tests all my language properties files? I want to test that every language in my project contains all keys.
Use cases:
Figure out if so. added a key to the default file and forgot to add to any of the other language files.
Figure out if so. dropped a key in one of the files and forgot to drop it in all the other files.
It is not that difficult to write my own small maven plugin, but I would prefer an already existing one. Haven't found one so far.
Or: How do you test your files? Manually / automated / not at all?
Eric
You should give a try to the i18n-maven-plugin. In the build (process-resources phase), all your Java classes, JSP will be parsed to find all the i18n keys in your project (according to your pom).
The plugin will add all the i18n keys that are missing in all you bundles. There is also a strict mode that remove all the i18n keys that are no longer found in your application from your bundles so you can be sure that 100% of the keys are both used in your app and translated in every language.
For a working, real-life example, feel free to check out this application:
svn checkout https://svn.codelutin.com/wao/tags/wao-4.0.4/
mvn clean process-resources -Di18n.verbose
Funny - I gave my project the same name a couple of years ago. https://github.com/hoereth/i18n-maven-plugin
This plugin serves me well on numerous projects. It turns around the concept of properties files 180 degrees. You maintain a well structured XMl file with your translations and the plugin will create all properties files for you during build time. No need for validation at his point. It can also create a Java class which holds all translation keys - thus enabling you to compile-check your translation calls.
Believe me - this takes away the pain of translating from a technical point of view. :)

SonarQube excluding files, directories, and generated code?

The code base I am working with has a lot of generated code. In addition, there are also some deprecated files that I would want to exclude from SonarQube analysis. I've read up the documentation and looked at some answers on here about that, but it does not help in my case.
I have a multi-module maven project. So I have multiple projects in my workspace that are all part of a large application. Say I want to exclude this file:
/home/username/workspace/com.mst.rtra.importing.message/bin/com/mst/rtra/importing/message/idl/parse/idlparser.java
I don't really know how to write this in the exclusions settings on SonarQube because of how long the filepath is. Also, what if I want to exclude another file, but from a different module, say :
/home/username/workspace/com.mst.rtra.interpreter.create/
I am confused about I should write this in the exclusions box in project settings. Should I write the absolute file path due to the multi-module nature of this project? Or is there some other convention used?
In addition, if I want to exclude generated files from analysis, I would need to put file:/generated-sources/ as I saw in another answer. However, after analysis, I can still view the analysis results of those files when I open up the project in SonarQube dashboard.
We use ant rather than maven, and an older version of the Sonar ant task at that. But what works for us is setting a sonar.exclusions property in our build.xml, which accepts wildcards for filenames. For example:
<property name="sonar.exclusions" value="**/com/ex/wsdl/asvc/*.java,**/com/ex/wsdl/bsvc/*.java"/>
That skips analyzing all the code generated from a wsdl file for two services. You ought to be able to do something similar for maven.

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.

How do you start with writing a mavenized Java application

I am pretty new to Java and Maven and today was studying about maven, how to Mavenize a java app, how to make it good for EClipse, etc... so now I have a question: when we want to write a Java and we know that we want to use Maven too, then we start writing application and creating out directory structure with the layout that Maven likes it? or we just write our app and at the end we mavenize it? Where do we start from when we want to create a java app from beginning and we want it to be mavenized too.
Use Maven from the beginning. Why write in a different structure and convert it again at the end, while you can do it in correct way from start? If you don't use Maven from the start, what build tools you will use then? Ant? I don't see any gain in productivity in such approach.
As Adrian said, you should definitely use Maven since the beggining.
One of its main value is to guide you on "the right way". I should say "The right Maven way". It's sometimes very restrictive, but it's the main advantage : if you do like Maven is waiting for, you are probably on the way that every community or big project do.
If you start your project "by yourself", you may forgot or miss something that will trouble you. For instance, main java sources are expected to be in src/main/java. Most of the plugin use the variable sourceDirectory to bind it ... but you may have to redefine this variable many times for plugins.
Another instance, and quite often confusing, is the multi module project. If you have 2 or more project that are binded (in their life cycle), you should set up a multi module project. If you don't, you will face of code or action duplication (2 tags, 2 build, 2 deployement, etc...).
So, in order to give you a straight answer :
read http://www.sonatype.com/books/mvnref-book/reference/
use the more appropriate archetype to build your project
if you create it under command line (not with an IDE), import it
then work :)
What I'm saying will appear you more and more clear using Maven :).
See Why does Maven have such a bad rep? to better understand why :)

Resources