Best way to migrate an IntelliJ project to Maven? - maven-2

I have a project that uses the built-in IntelliJ build system. What's the quickest/best way to migrate the project to Maven?

What's the quickest/best way to migrate the project to Maven?
Manually. Here is what I would do (I would personally use the command line):
create a parallel (potentially multi-modules) project structure using archetype(s)
declare dependencies for the jars you're using (not all of them, you'll get some transitively)
move sources and resources to the new structure
test the build
import the project back into your IDE

IDEA understands Maven very well. Unfortunately it does not create Maven configuration for you.
You'll have to write pom.xml yourself. The complexity of this depend on the structure of you project and frameworks/libraries that you use.
For me the easiest way is to create a new project based on artifact and then copy existing source files into it. You can do this in IDEA:
File -> New project -> Create project from Scratch
Choose Maven module, Next
Check Create from Artifact: a list of available artifact will show. Choose one based on libraries/frameworks you use. You can also add one if it's not on the list (search the web).
When project setup is done, start copying sources.

Related

How to add a jar to the external library for a project w/o disappearing after sync and with imports working?

Presently having an annoying time getting a jar added to the external library for the project, then in turn not disappear when syncing and the imports working. Adding I figured out and answered in another question. The issue is to keep persistent and accessible to a jar I create within the project for import.
I am not sure if I am missing a step or forgot to set something. I am using a modified gradle (forgegradle) to create the project. Have not found a solution with the documentation, or any for IntelliJ yet. I am generally new with IntelliJ, choosing to use it instead eclipse which I have previously used.
The goal is a to create an add-on for a mod to another jar. The main jar already in the external library from the start, now attempting to add the mod to it. I could modify the mod, but it is not my code, so rather not simply modify it directly and repackage it.
a jar added to the external library for the project, then in turn not disappear when syncing and the imports working
If you are using an external build tool / system that syncs with IntelliJ it is recommended to use that build tool / system to add dependencies.
IntelliJ will always take a backseat, and treat the build tooling / system as the source of truth for the project model as best it can.
You mention you are using gradle, I would recommend adding the dependency as a managed dependency that is, let gradle download it from a repository, and resolve the dependency/library itself.
If you can not do this, and you can't host a repository yourself, The next best recommendation I have, is creating a 'libs' folder inside the project, that contains jars that can't be found in repositories (They may be mods or plugins that were never published)
You can read up on how to add a library folder to gradle here: How to add local .jar file dependency to build.gradle file?

Several artifacts, how to force build order?

I have a (standard non-maven, non-gradle, non-whatsoever) project in IntelliJ IDEA that consists of several modules.
One of those modules results in a jar that is used by one of the other modules.
I have two artifacts. The first one creates a war file. This one depends on the jar file built from the second artifact.
How can I order the build process of the two artifacts so that the second one creates the jar file and copies it to the lib folder of the first, before the first one builds, without the need to recreate both artifacts?
As soon as I select "Build/Build Artifacts/All Artifacts" it always tries to build the first one first.
EDIT: Maybe a better question: What is the recommended way to manually build several artifacts in order of their dependencies?
How can I [configure IDEA] ... so that [it] ... creates the jar file and copies it to the lib folder of the first...
You can't really configure IDEA to do this directly. While you can configure Artifacts in the Project Structure dialog, there are no provisions for copying artifacts. IntelliJ IDEA is an IDE, not a build tool. While it can do a lot regarding complying and building, it has its limits.
One possible hackish way would be to go to the Artifact definition in the project structure. There, there are "Pre-processing" and "post-processing" tabs, They have the option to run an Ant target. So you could create a simple Ant target to do the copying. But in the end, I think the best answer to your question:
Maybe a better question: What is the recommended way to manually build several artifacts in order of their dependencies?
is to use a build tool such as Ant, Maven, or Gradle for building the project.

create project from maven2 repository

I want to create a maven2 project from artifacts in maven2 repository (POM and sources). Of course,I can create the directory structure, copy POM file and extract the sources manually. But I wonder if I can automate this task with maven.
Maybe dependency:unpack-dependencies can help you? Note, there are parameters such as classifier, copyPom, outputDirectory, type, etc that can be used to configure the content of the output.
Perhaps Maven archetype plugin can serve your need? It allows you to create an archetype, which can then be used to create a project.

How to convert Ant project to Maven project

How to convert a Ant project to Maven project? A sample project that would link (a Wicket project)
Thanks
The nice part of using maven is that most standard stuff works automatically once you do things the maven way. For a simple webapp:
Create a pom with groupId, artifactId and version (packaging: war)
Add the required dependencies to the pom
move the
java sources to src/main/java,
resources to src/main/resources,
webapp content to src/main/webapp,
test content to src/test/java and src/test/resources
set the compiler compliance version using the maven compiler plugin
That should get you up 'n' running.
http://www.sonatype.com/people/2009/04/how-to-convert-from-ant-to-maven-in-5-minutes/
I don't know what your ant script looks like, but assuming its a basic script for building, you will need to create a pom.xml file for your project, add your dependencies, and then build it via maven.
For anyone who lands here in future, there is an easier way to find dependencies for maven using the file hashes. So, you won't have to guess artifact versions.
As per the below article, the idea is to generate a SHA1 checksum of the dependency that you want to find the information, then do a reverse search in Nexus repository manager using that hash. For the checksum generation, you can use Microsoft's FCIV (free) utility.
https://devreads.xyz/ant-to-maven-conversion-the-painless-method/

How to configure IntelliJ IDEA and/or Maven to automatically add directories with Java source code generated using jaxb2-maven-plugin?

In our project we are using jaxb2-maven-plugin to generate Java source code from XML Schema, which causes that our Maven module requires additional source code directory (in our case target/generated/main/java). Up to date I've been using Eclipse and maven-eclipse-plugin to import all the projects into Eclipse workspace. The plugin is (somehow) able to add the directory with generated source code automatically to Eclipse' .classpath file.
Recently I try to switch to (play with?) IntelliJ IDEA 9 (so I am a newbie in this environment) and I've noticed that additional source directory is not added during IDEA's importing process...
Is there any way I can configure IDEA/Maven to make importing directory with generated source code automatically?
The convention with Maven is to generate code in target/generated-sources/<tool>, for example target/generated-sources/jaxb2
Follow this convention and IDEA will add the folder as source folder (see IDEA-53198).
Generated code, using jaxb2-maven-plugin, was missing for me in Intellij 2017.1 whereas Eclipse Neon created it.
Fixed it from context menu of module by selecting 'Maven -> Generate Sources and Update Folders'.
Try with maven-jaxb2-plugin. If it does not work then it's IDEA problem.
In Maven you can add new source roots per configuration. Maven plugins can do this programmatically. This is for sure the case with maven-jaxb2-plugin. Then, if IDEA does not recognize it, then it's a problem on that side.
You can use the Maven Build Helper Plugin. It is located at http://www.mojohaus.org/build-helper-maven-plugin/
It allows to configure additional source roots. And the maven integration of IntelliJ will add the new source root. We are using this feature in quite a few builds and it works just fine. Tested with vers. 13 of IntelliJ IDEA.