0

I am packaging my application as

mvn package

It creates a jar file by the name JasperCSVDataSource-1.0-SNAPSHOT.jar

When I try to run the jar file as :

java -jar JasperCSVDataSource-1.0-SNAPSHOT.jar

I get an error saying no main manifest attribute, in target/JasperCSVDataSource-1.0-SNAPSHOT.jar. To automatically add manifest file in the created JAR, I updated my pom.xml by adding the following:

<configuration>
  <archive>
    <manifest>
    <mainClass>com.comp.main.CommandLineRunner</mainClass>
    </manifest>
  </archive>
</configuration>

The complete pom.xml looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.comp</groupId>
    <artifactId>JasperCSVDataSource</artifactId>
    <version>1.0-SNAPSHOT</version>

    <configuration>
    <archive>
        <manifest>
        <mainClass>com.suhail.main.CommandLineRunner</mainClass>
        </manifest>
    </archive>
    </configuration>

    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.8.0</version>
        </dependency>

        <dependency>
            <groupId>ar.com.fdvs</groupId>
            <artifactId>DynamicJasper</artifactId>
            <version>5.0.11</version>
        </dependency>
    </dependencies>    

</project>

But now when I try to build the file, I get an error saying:

    [ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] Malformed POM /Users/tambro/jasper-test/pom.xml: Unrecognised tag: 'configuration' (position: START_TAG seen ...</version>\n    \n    <configuration>... @12:20)  @ /Users/tambro/jasper-test/pom.xml, line 12, column 20
@ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.tambro:JasperCSVDataSource:1.0-SNAPSHOT (/Users/tambro/jasper-test/pom.xml) has 1 error
[ERROR]     Malformed POM /Users/tambro/jasper-test/pom.xml: Unrecognised tag: 'configuration' (position: START_TAG seen ...</version>\n    \n    <configuration>... @12:20)  @ /Users/tambro/jasper-test/pom.xml, line 12, column 20 -> [Help 2]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException

I could not understand the reason for this. Is this not the correct way of adding manifest file to the jar?

Amanda
  • 2,013
  • 3
  • 24
  • 57
  • I think this question has already been aswered here: [Maven not recognising configuration in pom xml](https://stackoverflow.com/questions/38804534/maven-not-recognising-configuration-tag-in-pom-xml) – George Apr 24 '19 at 06:42
  • Here: https://maven.apache.org/guides/mini/guide-configuring-plugins.html – lealceldeiro Apr 24 '19 at 06:43

1 Answers1

0

You need to provide <configuration> tag with specific maven plugin. In this case it could be maven-jar-plugin So your pom.xml should have something like inside your <project> tags

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    ...
    <configuration>
      <archive>
        <manifest>
          <mainClass>com.comp.main.CommandLineRunner</mainClass>
        </manifest>
      </archive>
    </configuration>
    ...
  </plugin>
</plugins>
</build>
kalimba
  • 408
  • 4
  • 14
  • Could `artifactId` and `groupId` be anything? – Amanda Apr 24 '19 at 06:46
  • No it should be a valid maven artifact. – kalimba Apr 24 '19 at 06:47
  • Did not understand. What does it have to do with the `manifest`? – Amanda Apr 24 '19 at 06:47
  • it is plugin's responsibility to make final `jar` file so it is possible to provide information about `mainClass` as well, whose `main` method should be executed when you run `java -jar ` More info about this plugin: https://maven.apache.org/plugins/maven-jar-plugin/index.html – kalimba Apr 24 '19 at 06:54
  • I get an error saying `Unknown lifecycle phase "packge". You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test ` – Amanda Apr 24 '19 at 06:57
  • as I try to build – Amanda Apr 24 '19 at 06:57
  • you are giving wrong spelling of package. do `mvn package` – kalimba Apr 24 '19 at 06:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192298/discussion-between-kalimba-and-amanda). – kalimba Apr 24 '19 at 07:10
  • @Amanda please accept the answer if it is helpful – kalimba Apr 24 '19 at 07:10