Can I make a Maven plugin that generates a project and then builds that project? - maven

Is it possible to combine the capabilities of an archetype and a normal Maven plugin into a single plugin?
I have a custom language which I can compile into Java source code. I've written a Maven plugin which does this in the generate-sources phase, adds the Java source to the project, and builds the project. It works as I'd expect.
However, to use it, I need to first write out a pom.xml file referencing my plugin and describing where the input files live. I'd like to be able to go straight from raw input files to compiled code in a single maven command.
For example, suppose I have this directory structure:
my-project/
some-input-file.dsl
I want to run
bash$ mvn com.waisbrot.plugin:generate -DgroupID=com.waisbrot package
and after Maven's done running have:
my-project/
some-input-file.dsl
pom.xml
target/
generated-sources/
plugin/
SomeInputFile.java
classes/
com/
waisbrot/
SomeInputFile.class
some-input-file-1.0.jar

Actually, the integration testing of the archetype allows you to declare the parameter and goals. So do this:
Pick the template project you want to create
mvn archetype:create-from-project. It will create a new archetype
Review src/test/resources/projects, especially goal.txt and archetype.properties (source: http://maven.apache.org/archetype/maven-archetype-plugin/integration-test-mojo.html). Tweak so install will be implicity
mvn verify will be able to build the archetype, run the it, and get it installed
Hope it helps

Related

How to run just generate goal from swagger codegen maven plugin

I am using the swagger codegen maven plugin to generate a server stub from a swagger spec. If I run mvn compile then it generates properly and compiles the project. However, sometimes I want to run just the generate.
Specifically, I'd like to avoid both compiling the whole project and also running another plugin (checkstyles) which runs in the validate phase. Ideally, I'd like to generate the generated classes from the swagger spec and compile those classes but not the project as a whole.
The use case here is that while developing I may need to update the spec and re-generate at points when the project as a whole isn't valid (or won't be, with the new spec). I've read that you can use "prefix:goal". I've tried the following, but none work:
mvn swagger-codegen-maven-plugin:generate
mvn swagger-codegen:generate
mvn swagger:generate
mvn codegen:generate
It gives (e.g.)
No plugin found for prefix 'codegen' in the current project and in the
plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available
from the repositories
I also read that you could use "groupid:artificatid:version_goal" so I tried:
mvn io.swagger:swagger-codegen-maven-plugin:2.4.0-SNAPSHOT:generate
This seems to get further but gives an error
Failed to execute goal
io.swagger:swagger-codegen-maven-plugin:2.4.0-SNAPSHOT:generate
(default-cli) on project com.carus.api.base: The parameters
'inputSpec', 'language' for goal
io.swagger:swagger-codegen-maven-plugin:2.4.0-SNAPSHOT:generate are
missing or invalid
My pom structure is slightly complex. There are several APIs. Each is in some ways their own project. They have a common parent pom with the execution goal in, and the project pom only sets certain variables. The variable used as inputSpec is defined in the project pom, but the one for language in the base pom. (The .base project mentioned here is actually where the base pom is, and is an abstract package (<packaging>pom</packaging>). If I specify a specific project I get the same error but referring to that project.
Questions:
Can I even achieve what I want (generate code from the spec and
compile just that code but not the whole project) with a goal?
How to find out what "prefix" to use for the "prefix:goal" syntax?
The closest I have come is run mvn generate-sources -Dcheckstyle.skip=true. This generates but I don't think it compiles the generated classes. So I then have to clean the project in Eclipse to trigger it to recompile.
How to find out what "prefix" to use for the "prefix:goal" syntax?
The prefix for the plugin should be in this file: "swagger-maven-plugin-2.3.1.jar/META-INF/maven/com.github.kongchen/swagger-maven-plugin/plugin-help.xml".
<plugin>
<name>Swagger Maven Plugin</name>
...
<goalPrefix>swagger</goalPrefix>
...

Generated code is not compiled

I wrote a Mojo that creates a new Java class and puts it in /target/generated-sources/annotations. in addition, I have configured build-helper-maven-plugin to declare that folder as source folder.
The problem is when I do: mvn clean install from CLI it generates the source file but it doesn't compile it.
Note, if I run Maven Install from within Eclipse (using the m2e connector) then it works fine.
What am I missing?
Without an actual plugin definition, we can only speculate.
I can't comment on m2e, I see one obvious difference that you state by yourself: mvn clean install vs mvn install, but from the "bare" maven's standpoint,
here is one possible reason:
Maven has a concept of phases that comprise the lifecycle. An information about phases of default lifecycle is available here
Plugins (more precisely the "goals" of plugins) are something that usually gets attached to a particular phase.
Maven compiler plugin is attached to the compile phase, for example.
So, maybe the plugin that you've developed runs later than the compiler plugin.
Usually, source generation plugins are attached to generate-resources phase.
Its possible to run a full lifecycle in maven, in fact its what people usually do, for example, running mvn test actually means, run all phases of the default lifecycle up-to (including) phase test.
It's also possible however to run a particular plugin goal "directly" without attaching it to the phase. Of course, in this case, its pre-conditions should be met.
For example, mvn surefire:test means that we should invoke the surefire plugin directly. Of course the source code and test code should be compiled beforehand (bytecode has to exist in the target directory)
So I suggest you to run the following series of commands (Adjust if you have tests):
Run mvn clean install on the plugin project to place it to local m2 repo
Run the plugin directly: mvn ::: and check
whether the source is generated in the target folder
Make sure in pom.xml of the project that the source folder are configured correctly
Run mvn compile (phase up-to, including, compile) on pom even without the plugin
After this phase, there will be compiled sources in the target directory. Don't run
clean because it will wipe out all the target directory
This will effectively help to make sure that plugin does the job right

How to convert a Maven build to Gradle?

I know I should be working with my build.gradle and init.gradle files but I don't know what to write or how to point to my project folder with the pom.xml file.
The first step is to run gradle init in the directory containing the (master) POM. This will convert the Maven build to a Gradle build, generating a settings.gradle file and one or more build.gradle files. For simpler Maven builds, this is all you need to do. For more complex Maven builds, it may be necessary to manually add functionality on the Gradle side that couldn't be converted automatically.
https://guides.gradle.org/migrating-from-maven/
as Peter Niederwieser said:
For more complex Maven builds, it may be necessary to manually add
functionality on the Gradle side that couldn't be converted
automatically.
although you have to write some parts manually by your self. there is an online service that may be an useful tool For complex Maven builds. maven2gradle is a project on github which can convert online dependencies element automatically from maven to gradle scripts.
for using it,
get to maven2gradle . URL
open and select contains of your maven file.
Paste your maven dependencies on the text box in that web page (with
or without the dependencies root element).
click Convert button.
for more information http://sagioto.github.io/maven2gradle/

How to add dependency to my pom.xml?

I want to use https://code.google.com/p/droidpersistence/source/checkout but I don't know how add to my pom.xml..
The link you provided specifies a place you can download some code, using Subversion:
svn checkout http://droidpersistence.googlecode.com/svn/trunk/ droidpersistence-read-only
So run that command, and it will download the code. That particular code is designed to be built with ant, instead of maven. You need to write a little pom.xml file for it, so that when you build it on your computer with "mvn clean install", maven will generate a .jar file (the artifact), and put it in your local maven repository (.m2 directory). Then add a dependency on that jar to your pom file.
In general, to add a dependency to your pom using the latest version of IntelliJ Idea (12.1.6), click somewhere in your pom file, and press ALT-INSERT, then choose "dependency".
Hope this helps!

How to add files to a maven project executing generate goal (mvn archetype:generate)?

I'm building a maven archetype project. As parameter (serviceDescriptor), I'm passing path to an xml file. When the generate goal is successfully executed, I would like to have the serviceDescriptor file in src/main/resources. Based on maven archetype documentation, it seems that is not possible but, there should be a way to do it.
I have spent couple of days on this and I think that I have found a reasonable solution.
As I mention in the question, I'm passing the file path as required property to the archetype:generate.
I had to implement a simple plug-in that is executed after archetype generate is finishing. This plug-in is coping the file into src/main/resources, read some data from the file and update the pom.xml setting some properties. In order to be able to modify the pom.xml file I'm using maven-model-2.0 archetype as dependency in maven plug-in. It offers Maven MvenXpp3Reader and MavenXpp3Writer classes that allows to safe modify pom.xml.
In order to tell to archetype project to execute plug-in at the end of generate phase of archetype:
mvn archetype:generate -goals=plugin_groupId:plugin_artifactId:goal
The downside is that the plug-in should be available in a accessible repository or local repo.

Resources