Building jar with maven-scala-plugin - eclipse

I created scala application and now I want to build jar.
I run mvn package than I try to run jar by command
java -jar target/burner-1.0-SNAPSHOT.jar
and I see error:
Failed to load Main-Class manifest attribute from
How can I define Main-Class property?
Do I need to create Manifest.mf? where?
Or I need to have mainclass property somewhere in pom.xml?
Update:
I have created src/main/resources/MANIFEST.MF file with contents
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: itsabear
Main-Class: ru.dmteam.App
Build-Jdk: 1.6.0_20
I did not forget line ending at the end of file.
after mvn package I see new jar. I checked manifest.mf in this jar - it contains right main-class but when I type java -jar target/burner-1.0-SNAPSHOT.jar I still see an error Failed to load Main-Class manifest attribute from
My pom.xml http://pastie.org/1070483
UPDATE 2
I discovered that now there are two manifest.mf files in the jar.
MANIFEST.MF and META-INF/MANIFEST.MF
I moved my custom MANIFEST.MF to just created META-INF folder(in src/main/resources) but now mvn package overrides it while creating jar...

After creating a new maven project using the scala-archetype-simple archetype (A simple project that prints 'Hello World'), I needed to add the following to my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>test.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
for the class test.App to run as desired when invoked with the command
java -jar ./target/mytest-1.0-SNAPSHOT-jar-with-dependencies.jar
After running the command
mvn package

you can run the jar this way
scala -cp target/projectname-1.0-SNAPSHOT.jar

Related

Having only a JAR Maven Archetype file, how can it be generated a new project?

I'm just starting with Maven 3 for an Scala project in IntelliJ.
I have generated a JAR file following this guide.
I moved archetype.jar to a directory in where I want to create a new project. But my questions are:
Is this file stand-alone? Is it enough? It does not work with the command "mvn archetype:generate"
Is it possible to use the jar file without the intervention of any repository? So I can share it with collegues.
What's the best method for this, I've been reseaching and all the guides are based on repositories only and not in working local. Even the local repositories only consists in xmls files with the id but not the contents.
This is a sort of complex question you are asking...I am going to try summarise what I know and let's see if it helps.
Answers to your questions:
1) If you have just the basic Maven Project structure after you have generated the archtype and so on, if you run maven clean install and the project produces a jar, this in theory should be immediately executable from the command line and standalone.
However, as you add dependencies to your small projects, not all dependencies are automatically built into the standalone jar, sometimes you have to tell maven to bundle them into it.
Maven Shade Plugin - adds all the needed dependencies into your jar
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>MainApp</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>launcher</shadedClassifierName>
</configuration>
</plugin>
</plugins>
</build>
2) You do not need to integrate to any repository, its recommended for wider projects so that you can publish artifacts to your firms Repo for eas of use between developers
3) The easiest method is to create a new project in Intellij itself specifying Maven as the project type and that will give you the default project structure. In the pom file you then specify the build block I pasted above and you are essentially good to go...If you need Dependencies also t run your own code you will need to add them in a Dependencies block.
So, I finally used the following command before generating:
mvn install:install-file -Dfile=<path-to-jar-archetype-file> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar
After executing the command, the jar is installed in your local repo. You can generate the project with for instance IntelliJ or go to the path where you want to generate the project and...
mvn archetype:generate

Unable to get external class files of scala in build jar eclipse

I'm trying to create a runnable jar from scala spark project which consumes redis library. So, I created classes from Scala-Redis github project and used the package in my present code. The project builds and runs through eclipse interface but When I try to create jar - I receive classNotfound exception. On checking it was noticed that redis class was not included in build jar. I'm using other external jars as well. They are included in build but not the redis one. Where I'm going wrong or missing ?
Pom.xml is :-
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.spark.ReCalculateOdo</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
I've included the class folder in build path run configurations. Suggestions please, Thanks,
I tried one trick and it worked. I don't know why this happens but its working for my case. So the idea is whenever you want a jar just clean build the project then run the project in eclipse now create maven install. You will find a jar in target folder of project. So , now you can use it with command line. It'll work.
Thanks,

How to change jar output directory when running as maven install in Eclipse?

I am making a Minecraft plugin for Bukkit 1.8, and everything works fine. I right click on the project name > Run As > Maven install. It outputs the .jar file to the target directory. I then copy the file to the plugins folder of my Minecraft server.
I would like to have it output the jar directly into my plugins folder.
A simple way to do that would be to bind an execution of the maven-antrun-plugin to the install phase. This execution would copy the main artifact to the Minecraft server folder.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}.jar"
todir="/path/to/server/plugins" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
(This snippet must be placed inside the <build><plugins> element).
Running mvn clean install (or "Run As... > Maven Install" in Eclipse), Maven will do what you want. ${project.build.directory}/${project.build.finalName}.jar refers to the main artifact present in the build directory (which is target by default). You'll need to update the path to the server in the snippet above.

How to change default properties in Maven Release Plugin for WSDL builds

I require the default properties of mvn release:prepare and mvn release:perform to be modified. The source code i'm trying to build has a WSDL files.
The JAR file that is generated is in non maven format and this was achieved 2 step build process, using this I'm able to generate the maven based WSDL compiled JAR file.
mvn exec:exec
mvn install:install-file -DgroupId=com.stackoverflow.abc -DartifactId=xyz -Dversion=${BUILD_VERSION} -Dpackaging=jar -Dfile=path-of-jar -DpomFile=path-of-pom.xml
This builds the JAR file with just MAINFEST.MF and with no pom.properties and pom.xml, What i'm trying to do is convert the JAR generated in non maven format to maven format.
When I try using release:prepare and release:perform, I'm unable to override the default properties as used in mvn install:install-file and there is no way to generate the JAR with pom properties
Is there a way i can override the mvn release plugin properties that would help me to build the JAR files?
Plugin used to build the WSDL:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<extensions>false</extensions>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>com.sforce.ws.tools.wsdlc</argument>
<argument>src/main/resources//Enterprise.wsdl</argument>
<argument>target/wsdl-${version}.jar</argument>
</arguments>
</configuration>
</plugin>
What i've tried using is:
mvn -s settings.xml -Dusername=${svn_user} -Dpassword=${svn_password} release:prepare exec:exec release:perform -DgroupId=com.stackoverflow.abc -DartifactId=xyz -Dversion=${BUILD_VERSION} -Dpackaging=jar -Dfile=${WORKSPACE}/target/wsdl-${BUILD_VERSION}.jar -DpomFile=${WORKSPACE}/pom.xml
Below plugin is required to build the WSDL files, post that usual mvn clean install should do the job
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<extensions>false</extensions>
<version>1.2.1</version>
<executions>
<execution>
<id>custom-compile</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>com.sforce.ws.tools.wsdlc</argument>
<argument>src/main/resources/com/salesforce/wsdl/abc.wsdl</argument>
<!-- <argument>target/salesforce-wsdl-${project.version}.jar</argument> -->
<argument>target/salesforce-wsdl.jar</argument>
</arguments>
</configuration>
</plugin>

GWT compilation is skipped in maven when module packaging is pom

I am trying to reuse a assembled gwt compilation in another war. For this i am try to change the current maven module's packaging from war to pom. I then plan to use maven-assembly-plugin to zip up gwt's module js output and use that later on in another war module.
I tried changing the packaging tag from <packaging>war</packaging> to <packaging>pom</packaging> in pom.xml from Validation sample . gwt-maven-plugin never enters into compilation. Instead, it skips compilation!!!!!
What is happening?
Is this expected?
Is there any workaround?
To join multiple gwt compiled modules into a single .war file, it is very easy with the maven-dependency-plugin
Package all your gwt examples as habitual (.war), and install them mvn install or mvn deploy if you have a private maven repo.
Create an empty maven module of type war, with no code but with the maven folder structure, you can put any additional stuff you need here like a global src/main/webapp/index.html.
Configure the new module to use the maven-dependency-plugin like shown below, and run mvn package:
<dependency>
<groupId>my.group</groupId>
<artifactId>example1</artifactId>
<version>...</version>
<type>war</type>
</dependency>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-gwt-examples</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>my.group</includeGroupIds>
<includes>**/example1/**</includes>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Finally and related with the gwt-maven-plugin, like with any other maven pluging, it would be enough to select an appropriate phase of the pom-packaging life cycle (package, install or deploy):
...
<packaging>pom</packaging>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
...
<configuration>
...
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
Unfortunately, gwt-maven-plugin specifically disallows compilation when packaging is pom, take a look to line #269 of CompileMojo.java
You can create the reusable modules (that you mention as samples in the comments) as separate GWT projects with no EntryPoint. Package them as jar and add the following as resources:
the client side source code
other resource items that will be necessary for the final compilation (images, xml files, etc.)
Something like this:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
...
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/services/**</include>
<include>**/client/**</include>
<include>**/public/**</include>
<include>**/*.gwt.xml</include>
</includes>
</resource>
</resources>
</build>
That's it, you can reuse it in any other GWT project. When you will do so, you just have to add the dependency (to the reusable module) in the pom.xml and import in the *.gwt.xml.
As for Maven's behaviour, it seems correct. pom packaging is going through package, install and deploy phases and
By default, the compile goal is configured to be executed during the ''prepare-package'' phase to run as late as possible.
You could change the phase in the plugin's execution, but I think it's risky because you can't know when exactly during the package phase will your code get compiled.