3

I'm new to maven and i recently developed a swing project while using maven to manage dependencies.

What I want to do right now, is package my project for distribution and be able to transfer the file to another computer/laptop for a person to open and use.

From my research, I understood that i need to package the project using assembly or shade maven plugin.

I have tried both and have been able to have an executable jar but i try to run it, nothing happens.

Here is my project structure HERE

Here is my POM.xml when using shade plugin:

<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>1</groupId>
  <artifactId>Fin</artifactId>
  <version>0.0.6-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>AinsfieldFin</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.14</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.14</version>
    </dependency>

    <dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j</artifactId>
    <version>2.8.0</version>
    </dependency>

    <dependency>
     <groupId>org.apache.xmlgraphics</groupId>
     <artifactId>batik-bridge</artifactId>
     <version>1.7</version>
     </dependency>
  </dependencies>



  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.Fin.index</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <Version>3.1</Version>
                </configuration>
            </plugin>

    </plugins>
  </build>
</project>

Here is the file index.java

package com.Fin;

//swing imports

import com.Fin.process.ManipulationExcel;
import com.Fin.process.ManipulationWord;


public class index {

    //declarations


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    index window = new index();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public index() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
          // present layout and app functions   
    }
}

and I am using eclipse as my IDE

Thank you in advance for any help provided

RESOLVED: My pom.xml looks like this now:

<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>1</groupId>
  <artifactId>Fin</artifactId>
  <version>0.1.1</version>
  <packaging>jar</packaging>

  <name>AinsfieldFin</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.14</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.14</version>
    </dependency>

    <dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j</artifactId>
    <version>2.8.0</version>
    </dependency>

    <dependency>
     <groupId>org.apache.xmlgraphics</groupId>
     <artifactId>batik-bridge</artifactId>
     <version>1.7</version>
     </dependency>
  </dependencies>



  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.Fin.index</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.Fin.index</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <Version>3.1</Version>
                </configuration>
            </plugin>

    </plugins>
  </build>
</project>
mushood badulla
  • 1,043
  • 1
  • 12
  • 19
  • Maybe something like [this](http://stackoverflow.com/questions/24899985/this-project-cannot-be-added-because-it-does-not-produce-a-jar-file-using-an-ant/24900260#24900260) might help – MadProgrammer Jan 12 '17 at 08:33

1 Answers1

3

You can do this by using this part inside your pom.xml (change your main class location:

 <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.your.mainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
Lefteris Bab
  • 787
  • 9
  • 19
  • Hello. Thank you for your help but I dont understand what you mean, if I change my main class location, it should work? I have replaced the part you told me, and i got another jar which when i run, nothing happens.. Just in case I am doing something wrong, I do "Run as" > "Maven Build..." > and then as goal "package" – mushood badulla Jan 12 '17 at 08:59
  • Hi, there you put your main class.Location like com.example.Class to the tag. You should do a mvn clean install and then go to target and try java -jar nameOfJar.jar – Lefteris Bab Jan 12 '17 at 09:09
  • That helped loads. Did that and was able to see my app giving off a null pointer error! I corrected the error and its working now! LIFE SAVER! – mushood badulla Jan 12 '17 at 10:03