How to use custom plugin in maven pom.xml? - java

I created one custom plugin in maven. So when run this project then automatically created related one JAR file. So now i created one more project in maven in this project how to use that jar file in the pom.xml file. Is there any possible to use already existing jars in pom.xml file.

I'm a little unsure of what you're getting at- my understanding is that you wrote a plugin that you now want to use in a separate project. In order to do this, you would add the custom plugin to your section in the new project. It will resolve this the same way it does all plugins. You also need to deploy the plugin to whatever pluginRepository you're using.
For example, I have a plugin, which is defined in its pom as:
<groupId>org.mine</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>1.0</version>
<packaging>maven-plugin>
which is what it seems you already have.
Then, run mvn deploy.
In the project you want to use the plugin is, add to your pom:
<build>
...
<plugins>
...
<plugin>
<groupId>org.mine</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
...
</plugins>
...
</build>
Finally, make sure you have the repository you deployed to set up in your pom.xml in the tag.

Related

Dynamically package JAR to specific directory for any given Java project using Maven and IntelliJ IDEA

I'm making Minecraft server (Spigot) plugins using IntelliJ IDEA and Maven. I want a button which works on all my Spigot projects (not necessarily automatically determining these projects, though that would also be useful) which packages my plugin directly to my testing server's 'plugins' folder then starts the server.
There are two methods I've thought of to accomplish this, but neither of them work due to limitations with IntelliJ and Maven.
My first idea was to write a batch file which takes the path to the packaged JAR as a parameter, copies the JAR from that path to the 'plugins' folder then starts the testing server.
move "%*" "C:\path\to\my\spigotTestingServer\plugins"
call "C:\path\to\my\spigotTestingServer\startServer.bat"
Then, in the 'Script parameters' for my run configuration, I would reference Maven properties (${project.build.directory}\${project.artifactId}-${project.version}) to obtain this path. However, IntelliJ doesn't seem to allow you to reference Maven properties in any run configuration settings.
My second idea was to modify the package directory directly inside my POM using Maven plugins. However, this means I'd still have to copy this code between projects and it would pollute my Git commits with a path only effective with my filesystem.
Now, I found out Maven has 'build profiles' which could potentially be a solution to this, so I wrote this 'settings.xml'
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>spigotTestingServer</id>
<build>
<finalName>${project.name}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>C:/path/to/my/spigotTestingServer/plugins/</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</settings>
I could then use this for all my Spigot projects by adding '-P spigotTestingServer' to the run configuration.
However, Maven doesn't allow profiles defined in settings.xml to include anything under 'build' including build plugins, which is exactly what I need to modify the package directory.
So, at this point, I'm stuck. Is there any way to get around the issues I've faced so far or are there any alternative solutions to packaging my JAR directly to my Spigot testing server's 'plugins' folder?

Convert maven to gradle?

I have a project in maven. I want to convert project to gradle project how convert this code maven to code gradle:
<build>
<plugins>
<plugin>
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.9.1</version>
<configuration>
<functionTarget>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</functionTarget>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
First install Gradle on your machine.
Now, go to your maven project’s root directory and execute command:
gradle init
Please note that gradle init automatically detects the pom.xml and creates a gradle build with the Java and maven plugin loaded. It means that existing Maven dependencies are automatically converted and added to your gradle build file.
So that the build.gradle file will be automatically created thanks to the gradle init command.And now your project is using gradle
I've never used function framework, however when it comes to plugin translation, these are pieces of code usually written by the maintainers of the framework, and usually they want to provide plugins for both maven and gradle.
Indeed in the documentation of the project, there is section for maven:
See Here
And also for gradle:
See Here
It looks like they offer to register the task in gradle, so its the best you can do with the existing state of the project, so its the way to go I believe

Automatically install a project in the local repository?

I'm trying to work around a maven bug MDEP-187 ( https://issues.apache.org/jira/browse/MDEP-187 ) by not using workspace resolution.
This forces me to do a mvn install for all my dependencies, I'm doing this by creating a launch configuration in eclipse with goal install.
The problem is that i have to create a launch config for every project in my multiproject workspace, in addition to install i have to manually call every launch config and run it. Which just doesn't work.
Is it possible to automatically install a project in the local repository? (whenever i update my code)
If you don't need to run dependency:copy in Eclipse, you can use following work-around:
Add a profile to your pom.xml, something like this:
<profiles>
<profile>
<id>copy</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
[...]
</executions>
</plugin>
</plugins>
<build>
<profile>
</profiles>
Enable workspace resolution in Eclipse.
Then Eclipse will not use dependency:copy, but you can use dependency:copy with command line: mvn install -P copy.
I did go with #khmarbaise solution:
But than you need to can handle the whole thing via
maven-assembly-plugin which can create archives / folders with all the
dependencies. Apart from that a swing ui must be started somehow which
will need some kind of shell script / batch file which you can create
by using appassembler-maven-plugin...And it sounds like you need to go
for a multi module project in maven..cause you might have parts like
core, ui, etc. which are needed to be combined in the end.
#khmarbaise i was in the understanding that the assembly-plugin didn't
support putting dependencies in a lib/ folder (just putting everything
in 1 big jar), but after a little bit of trying i just go myself a zip
with a runnable jar and my dependencies in a lib/ folder. Tomorrow i'm
going to read a bit more about the assembly-plugin. I'm happy ;-

Another own Maven project as dependency - how to solve it?

I have three Java console applications - A, B and C, all are managed with Maven. Application A contains common tools classes and these tools are used by another two applications - B and C. I need to know how to solve this dependency with Maven.
I have an idea - please correct me if I am wrong, to package and install application A into local Maven repository via mvn install command and this library set as new dependency for app B and C in their pom.xml files.
Is this a good idea or is there some better way how to solve this issue?
This question seems to be related to my issue, but I'm not able to judge if is it a good solution in my situation - How do I add a project as a dependency of another project?
Yes, it's perfectly fine. This solution also scales - you may later install you artifacts to remote repositories or even Maven Central.
Some other option is to place these three projects as modules under a common parent POM with packaging set to pom. The advantage is that you would be able to install all three artifacts with a single command and i.e. update dependencies on-the-fly when using Maven Release plugin.
Yes, this seems like the right way to solve this problem. Alternatively you can deploy to a real repository. That would be even better if you want other people to build project B and C without installing A first.
I would put the code that B and C use in its own separate jar. That way B and C don't use all of project A, but just a library with the code that they need.
I would not use the advice of the linked article. It uses a system path which is not ideal because it needs an absolute path so it will only work if someone installs the projects in the same place on their file system as you.
Running mvn install will solve your problem, but then you are solving it manually. It creates many complications when your projects change. What you need is a pom.xml that makes references to your sub projects:
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<groupId>X</groupId>
<artifactId>X</artifactId>
<version>0.1</version>
<name>X</name>
<packaging>pom</packaging>
<modules>
<module>project_A</module>
<module>project_B</module>
<module>project_C</module>
</modules>
<build>
<defaultGoal>package</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>src/assemble/bin.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Put this in a directory that contains those 3 directories in which each of your projects are. Then you can call mvn clean package on this directory and Maven will magically solve the problem for you, building each project in the right order and making one project available to the others if there are dependencies.
BONUS: On the example above I am also adding a reference to a Maven Assemble, which you can use to pack all your projects into a single distribution.

How does Maven understand release:prepare command?

I'm trying to create a plugin which would download and install jars from Maven central as system tools. So I want my line to be like
mvn install-plugin:install org.chaschev:cap4j:1.0
similar to Ruby's
gem install capistrano
This plugin would gather all the needed information about the shortcuts to create from the JAR. I.e. this jar would contain a class implementing an installation interface.
How does Maven understand that in order to execute a command like release:prepare it requires to download the release plugin and to run it? Any better/other way to do this?
Do you mean how the relation between plugin/goal in the comamnd line and plugin implementation is defined? Then the answer is plugin.xml. See plugin.xml for release plugin, e.g. maven-release-plugin-2.0.jar:
<goalPrefix>release</goalPrefix>
...
<mojos>
<mojo>
<goal>help</goal>
...
<mojo>
<goal>prepare</goal>
...
Or do you mean, how Maven discovers which plugins are available? Then the answer is:
There are two default groups where plugins are searched, org.apache.maven.plugins and org.codehaus.mojo
For your own plugin you may want to use name ${prefix}-maven-plugin, e.g. cap4j-maven-plugin
You can keep your name cap4j, but then put the plugin description to your POM, under <plugins>
If you want your build to work at other machines, they should point <pluginRepositories> in POM or in settings.xml to your plugin repository
It is not good to use default Maven groups for your own project.
Instead, define your own group for your plugin, like this:
<pluginGroups>
<pluginGroup>org.chaschev</pluginGroup>
</pluginGroups>
And rename your plugin from cap4j to cap4j-maven-plugin. Then Maven will discover your plugin without further cahnges in POM.
Alternative, without <pluginGroups>, just put following to your POM:
<plugins>
<plugin>
<groupId>org.chaschev</groupId>
<artifactId>cap4j</artifactId>
<version>...</version>
<configuration>
...
</configuration>
</plugin>
...
</plugins>

Categories