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

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.

Related

Maven - create archetype which uses mojo for source generation

I have a MOJO which uses a configuration XML file and generates Java source files:
The input configuration file is put \src\main\<name of mojo>\, belonging to the project which uses the MOJO.
The output generated source files are put in the directory target\generated-sources\<name of mojo>\, belonging to the project which uses the MOJO.
Now I want to create (not use!) an archetype which uses the MOJO to generate its source files. This means that:
The end-user of the archetype must be able to pass the configuration to the archetype.
The archetype must be able to execute the MOJO to generate the source files.
Is this possible? To summarize, the flow is:
1) The end-user uses archetype to create a Maven project; end-user provides configuration file.
2) The archetype accepts the configuration file and uses the MOJO to generate the sources. The pom.xml and other resources are built-in into the archetype.
3) The end-user gets a Maven project, which he probably will treat as read-only.
In other words, the outcome of the archetype depends on the provided input (configuration).
According to me it is not possible to do what you are looking for. Instead
I propose you to look at partial achetype which allows to execute mvn archetype:generate on existing maven project.
In order to explain what it does. Imagine that you are working on a maven project and you want to apply a partial archetype on it. This partial archetype contains a pom.xml with some dependencies and a directory structure which contains some files.
To apply this partial archetype you run the command mvn archetype:generate inside your maven project.
Once applied both pom.xml files are merged and files and directory structure as well.
Technically a partial archetype is a regular archetype that contains a partial attribute to true inside its archetype descriptor file.
<archetype-descriptor
xmlns="..."
xsi:schemaLocation="..."
name="MyProject"
partial="true">
...
</archetype-descriptor>
https://maven.apache.org/guides/mini/guide-creating-archetypes.html

How do I set up a TeamCity build job to execute a maven job with no pom

We have an in-house developed MOJO that generates content and doesn't require you to have an existing project or POM. Think of the maven archetype plugin, where you can just run mvn [mojo]:[goal] and have maven just execute that goal without a POM.
This MOJO connects to a specific database instance in a specific environment, and generates some metadata for the contents of the database, so our testers can inspect the metadata and locate production-like data that has certain attributes they need for a given test.
When you execute the metadata mojo, maven resolves the MOJO from the available repo's (in our case an Artifactory repo), and it then does its work and returns. It does not create any artifacts or other outputs.
We use TeamCity as our CI server, but it also has metadata generation jobs so with one click a dev can kick of a metadata generation job against a specific database.
The problem with this is the Maven runner in TeamCity requires a POM. If TC hasn't already checked out a project from a VCS, or the project it's checked out doesn't have a POM, the maven runner won't do anything. In this case, there is nothing to check out (the MOJO is resolved from Artifactory) so there is no POM.
I can set up the TC job to use the Command Line runner and have it execute, say, mvn com.example:metadata-generate -DenvironmentName=UAT1, but then it's impossible to specify the maven settings file that maven should use.
So my question is, how do I do this? Is it possible to have the maven runner execute an arbitrary maven command without needing a POM? Alternatively, using the Command line runner, is it possible to have a TC job copy a specific maven settings file to the build agent so it can be referenced in the maven command as mvn com.example:metadata-generate -DenvironmentName=UAT1 -s {path-to-settings-file}?
So its turns out that TC handles pom-less maven builds just fine. My problem was that the MOJO was not declared to not require a project.
Comparing my MOJO with the MavenArchetypePlugin source, I needed to declare my MOJO with the class level javadoc tag #requiresProject false.
Once I had that in place, TC ran my pom-less job perfectly well. All I had to do was clear the Path to POM file: field in the TC build configuration and leave it blank.
You can customize the name of the pom file that you use as an argument into the maven build-step in the teamcity and use this as the second "build step".Lets call the parameter as pom.file.name
In the first step , resolve all the in-house dependencies that you have and set the name of the pom file you want to execute into the variable pom.file.name
If you want to know more about how to change tha value of a variable in teamcity, you can read about it here

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/

Maven archetype - Run external commands after creation

I'm looking for a way, to execute additional commands (e.g. a perl script) after running mvn archetype:generate on my custom archetype automatically.
Is this possible?
Context
I'm writing an archetype, that creates OSGi bundles which i want to integrate into a parent project as modules. After generating the bundle, i wish to organise it into the parents directory structure and then manipulate poms and other configuration files automatically.
This has been asked on maven forums before 1, however no answer was given.
Thrau, you can invoke mojo post execution of archetype:generate by adding -Dgoals="your custom mojo plugin", within the plugin you can write your custom code. Hope this helps.

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

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

Resources