Creating a Maven Project Programmatically Runtime - maven

I have created a number of java classes using sun code model library. Now, I would like to create a maven project add those classes and upload it to a Nexus Sonatype Internal Maven Repository. Does anyone know how to create a maven project programmatically. Any help will be appreciated.

It is not really programmatically, but maven archetype can generate a project squeleton for you:
mvn archetype:generate -DgroupId={project-packaging}
-DartifactId={project-name}
-DarchetypeArtifactId=maven-archetype-quickstart
You can also run simply :
mvn archetype:generate
And choose an archetype from the list displayed.
Once done, you can put your java file in src/main/java.
https://maven.apache.org/guides/introduction/introduction-to-archetypes.html

Related

How to build a maven scaffold like spring initializr

As the title, is there such a maven plugin that can achieve such a function, I can manually select the required dependencies, or even generate the code automatically?
There is https://code.quarkus.io/ for generating projects from the browser and also the quarkus https://quarkus.io/guides/cli-tooling if you want to use a command line application
You can create Maven project with mvn archetype:generate command. For example:
mvn archetype:generate -DgroupId=com.roamch007 -DartifactId=my-app -DinteractiveMode=false
This command will create project in may-app directory.
If you need specific dependencies you can create Maven Archetype, and re-use this archetype while generating new projects.
Read more: Guide to Maven Archetype

Project creation based on custom archetype leaves out folders

I created my own maven archetype in my company's maven nexus.
The archetype jar file has all the necessary folders and pom.xml in the archetype-resources folder
But when I create a project based off of the archetype, the folders all get left out and only the pom.xml remains.
How can I fix this issue?
Thanks
Just as a note, I tried to create the archetype jar in several ways:
mvn archetype:create-from-project
mvn install
mvn deploy
mvn clean compile assembly:single
I still get the same issue

Maven 3 - Unable to find archetype

I have created a customized archetype, customA-web-archetype. Within the project folder, customA-web-archetype , I ran mvn clean install archetype:update-local-catalog which was successful. Now when I run mvn archetype:generate -DarchetypeCatalog=local outside the folder to use the archetype, I am getting the following error:
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
Your filter doesn't match any archetype (hint: enter to return to initial list)
FYI- When I was setting up Maven, I moved the .m2 to a different directory rather than using the default home directory. I made the appropriate change to settings.xml so that it can find the .m2 in the updated directory structure.
I am not using any repo manager such as Nexus. Just a local m2 repo sitting on the same box as the archetype project.
To run the archetype command, you need to specify the groupId, artefactId and version of your archetype, as well as those of your future maven project.
You should try the following command, with your own parameters:
mvn archetype:generate -DarchetypeCatalog=local -DarchetypeGroupId=yourArchetypeGroupId -DarchetypeArtifactId=yourArchetypeArtifactId -DarchetypeVersion=yourArchetypeVersion -DgroupId=projectGroupId -DartifactId=projectArtifactId -DinteractiveMode=false
After upgrading from Maven 2 to 3 we found the same problem. The fix was to change the version of the archetype-packaging and maven-archetype-plugin from 2.2 to 3.0.0 in the archetype pom.
I looks like maven moved the location of it's archetype repository file from ~/.m2 to ~/.m2/repositories which is why generate wasn't finding it.
One strange behavior I'm still seeing is that if I just do
mvn archetype:generate
it doesn't show my local archetypes, however if I do a
mvn archetype:generate -DarchetypeCatalog=local
it shows up. Hope this helps.
Have you tried crawling the repo to create a catalog? Perhaps it's missing / broken? Archetype plugin has crawl goal, which parses the repo and creates catalog.xml.
mvn archetype:help
[... snip ...]
archetype:crawl
Crawl a Maven repository (filesystem, not HTTP) and creates a catalog file.

How to create maven dependency in java?

How to create maven dependency in java?
what are the benefits of maven?
what is the role of pom.xml?
mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get
-DrepoUrl=http://download.java.net/maven/2/ -Dartifact=robo-guice:robo-guice:0.4-SNAPSHO
mvn install
Download the “kaptcha“, extract it and copy the kaptcha-version.jar to somewhere else, for example, c drive. Issue following command :
pom.xml
After installed, just declares the kaptcha coordinate in pom.xml.
Done
Build it, now the "kaptcha" jar is able to retrieve from your Maven local repository.
link :-http://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/

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