Running Maven project by adding dependency - maven

I am trying to run this DDR (Deep Dependency Representation) conversion tool from Emory NLP: https://github.com/elitcloud/elit-java/tree/master/elit-ddr
I have created a new Maven project in a new directory called depConverter and added the specified dependency from the website into the pom.xml file in DepConverter. This pom.xml file was the one built from automatically creating a new, default Maven project. I think I am missing something from this pom.xml file to properly run the DDR tool, as when I run the specified command line command java cloud.elit.ddr.bin.DDRConvert -i relcl.parse -oe tsv in depConverter, I get the following error:
Error: Could not find or load main class cloud.elit.ddr.bin.DDRConvert
I am not totally clear on how Maven will load this class. Do I have to create a java file to run this tool?

Assuming, the question is about running from the command line:
DDR library is not in the classpath when you're trying to run from command line. That's why JVM cannot load the main class that is expected to be found in this library.
So you can try to run:
java -classpath <PATH_TO_DDR_JAR> cloud.elit.ddr.bin.DDRConvert -i relcl.parse -oe tsv
This is the easiest way.
The jar is expected to be found in the local maven repository (~/.m2/repository by default).
Maven is a build tool in a nutshell and it doesn't run anything inside your artifact/ one of the dependencies like in your case.
Alternatively, if you want to "use" the information provided in pom (like a dependency graph), you still can run mvn exec:exec / mvn exec:java plugin and supply the relevant parameters to run the class of your choice.
You can find more information about exec plugin here.

Related

Setting name of jar file while using mvn install:install-file

So previously I was trying to find a way to install jar file which is built in my project to the .m2 folder via run configuration support.
Link for reference.
My main concern then was to not keep any hard coded values in command and to pick most data from pom.xml file. This was achieved successfully, but now I have another problem.
In the project, I have 2 modules module1 and module2.
When module1 is built, it generates 2 files a war file since it is a web based application and second one is jar file which is used to satisfy dependencies of other modules.
The jar file is generated using
<attachClasses>true</attachClasses>
property set in the maven-war-plugin in pom.xml of module1.
So if the module1 artifact id is set as module1-corp, then the jar file is named as module1-corp-classes.jar if the jar is installed using maven-install-plugin. But due to the legacy structure of the project, maven-install-plugin cannot be used and I have to use maven command line via Intellij run configurations to install this file.
So the command I used is
mvn install:install-file -Dfile=${project.build.directory}/${project.build.finalName}.jar -DgroupId=${project.groupId} -DartifactId=${project.artifactId} -Dversion=${project.version} -Dpackaging=jar
This installs the jar file perfectly, only it doesn't append the classes part at the end of jar file. so my jar file is now installed as module1-corp.jar instead of module1-corp-classes.jar which is not working okay with modules which are dependent on it.
I suspect this is due to the way module1 dependency is accessed in module2 which is as follows:
<dependency>
<groupId>module1.groupid</groupId>
<artifactId>module1.artifactId</artifactId>
<version>${module1.version}</version>
**<classifier>classes</classifier>**
</dependency>
This code is in the module2 pom.xml. I believe the classifier part is what is causing the issue, but I cannot change this since it is a legacy project.
So in the end I have two options only
Rename the jar while it is being installed via maven command line
Some other way which can rename the jar via an Intellij run configuration.
I tried using following flag
mvn install:install-file -Djar.finalName=jarname
But this doesn't seem to work as expected.
The install maven plugin allows also to specify the classifier (see: here). So in your example the command would need to be changed to:
mvn install:install-file -Dfile=${project.build.directory}/${project.build.finalName}.jar -DgroupId=${project.groupId} -DartifactId=${project.artifactId} -Dversion=${project.version} -Dpackaging=jar -Dclassifier=classes

Get the library via dependency code using bash command

I'm trying to make a library for projects using gradle and maven.
To test that if it is possible for the gradle to download it via the dependency code in the bintray, I need something like a bash command to see if the request gets the file from bintray or not.
TL; DR
Is there a terminal command for gradle to get a dependency like
./gradlew dependecies 'com.example.lib:module:2.0.0' ?
Download it's .pom file
You can download the library pom file from maven. All the library dependencies are in a pom file. Gradle uses this file to fetch the dependencies

How can I specify the directory where to create the project for archetype:generate?

I am using mvn archetype:generate -B ... to generate a maven project.
It places the generated project in the current working directory.
Can this be customized to place the generated project in a directory I specify? I don't see any options to do so in the command line --help menu.
You cannot specify a directory for archetype:generate, this plugin always targets the current working directory.
The Maven Archetype docs suggest using the basedir parameter. Something like this perhaps:
mvn archetype:generate
-DgroupId=foo
-DartifactId=bar
-Dbasedir=/some/other/directory
But, unfortuntately that doesn't work, even with that parameter specified the archetype is generated into the current working directory. Looking at the Maven Archetype JIRA I can see that there is an open issue for this:
https://issues.apache.org/jira/browse/ARCHETYPE-311
This issue has been open since April 2010 and it has been raised against version 2.2.0 of the archetype plugin and I have just verified that this issue still exists with in latest version of the archetype plugin, the following command completed but created bar in the current working directory rather than in /some/other/path ...
mvn org.apache.maven.plugins:maven-archetype-plugin:3.0.1:generate
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
-DgroupId=bar
-DartifactId=foo
-Dbasedir=/some/other/directory
So, if you want to use archetype:generate and you want the generated project to exist somewhere else then I think you might have to write a simple script which ...
Invokes the plugin
Moves the created directory to your desired location once the plugin has finished running
-Dbasedir doesn't work.
You could specify output directory by passing -DoutputDirectory=/some/other/directory
Here's the documentation
https://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html

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

What is the most common way to unpack a jar file after jenkins maven build?

I am new to maven and Jenkins so I do not know what is the most common way to extract the JAR file build by maven in the same Jenkins job.
Running mvn install in a Jenkins job outputs the file /home/user/.jenkins/workspace/$JOB_NAME/project/target/package-2.0.0.jar.
I want to extract it to some directory like /opt/project and call /opt/project/script.sh.
I thought of a post-build shell script calling jar -xvf <path>/package-2.0.0.jar but how to get the version number (2.0.0) then? Maybe there is a maven goal to do this?
define that artifact as a dependency in some other module (the module that will run the shell script) and use the dependency plugin to unpack it
that would mean you'd have (at least) 2 modules in your maven project - one that produces the jar, and the other that does something with the artifact produced by the 1st.
if that doesnt fit your need you could bind the unpack after the install phase (the artifact makes it into the local repository at the install phase, and the dependency plugin only deals with artifacts from the local repository) and do it there.
if youre still not satisfied you can get the artifact name in a maven pom.xml file by using ${project.build.finalName}. the default is ${artifactId}-${version} as you can see here (look at the super pom). if you need it with the suffix it'll be something like ${artifactId}-${version}.${packaging}
if you are running on linux based systems something like
jar -xvf `ls <path>/package-*.jar`
will do the job.

Resources