{"id":148,"date":"2011-10-29T13:42:34","date_gmt":"2011-10-29T03:42:34","guid":{"rendered":"http:\/\/www.docx4java.org\/blog\/?p=148"},"modified":"2011-10-29T13:42:34","modified_gmt":"2011-10-29T03:42:34","slug":"hello-maven-central","status":"publish","type":"post","link":"https:\/\/www.docx4java.org\/blog\/2011\/10\/hello-maven-central\/","title":{"rendered":"Hello Maven Central"},"content":{"rendered":"<p>With version 2.7.1, docx4j &#8211; a library for manipulating Word docx, Powerpoint pptx, and Excel xlsx xml files in Java &#8211; and all its dependencies, are available from Maven Central.<\/p>\n<p>This makes it really easy to get going with docx4j.\u00a0 With Eclipse and m2eclipse installed, you just add docx4j, and you&#8217;re done.\u00a0 No need to mess around with manually installing jars, setting class paths etc.<\/p>\n<p>This post demonstrates that, starting with a fresh OS (Win 7 is used, but these steps would work equally well on OSX or Linux).<\/p>\n<p><strong>Step 1 &#8211; Install the JDK<\/strong><\/p>\n<p>For the purposes of this article, I used JDK 7, but docx4j works with Java 6 and 1.5.<\/p>\n<p><strong>Step 2 &#8211; Install Eclipse Indigo (3.7.1)<\/strong><\/p>\n<p>I normally download the version for J2EE developers. Unzip it and run eclipse<\/p>\n<p><strong>Step 3 &#8211; Install m2eclipse.<\/strong><\/p>\n<p>In Eclipse, click Help &gt; Install New Software.<\/p>\n<p>Type &#8220;http:\/\/download.eclipse.org\/technology\/m2e\/releases&#8221; in the &#8220;Work with&#8221; field as shown:<\/p>\n<div><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/m2e-install.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-155\" title=\"m2e-install\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/m2e-install.png\" alt=\"\" width=\"735\" height=\"593\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/m2e-install.png 735w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/m2e-install-300x242.png 300w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/a><\/div>\n<p>then follow the prompts.<\/p>\n<p><strong>Step 4 &#8211; Create your Maven project<\/strong><\/p>\n<p>In Eclipse, File &gt; New &gt; Project.., then choose Maven project<\/p>\n<p>You should see:<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/new-maven-project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-157\" title=\"new-maven-project\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/new-maven-project.png\" alt=\"\" width=\"628\" height=\"423\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/new-maven-project.png 628w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/new-maven-project-300x202.png 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/a><\/p>\n<p><strong> <\/strong><\/p>\n<p>Check &#8220;Create a simple project (skip archetype selection)&#8221; then press next.<\/p>\n<p>Allocate group and artifact id  (what you choose as your artifact id will become the name of your new project in Eclipse):<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-allocate-group-and-artifact-id.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-158\" title=\"maven-allocate-group-and-artifact-id\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-allocate-group-and-artifact-id.png\" alt=\"\" width=\"628\" height=\"610\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-allocate-group-and-artifact-id.png 628w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-allocate-group-and-artifact-id-300x291.png 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/a><\/p>\n<p>Press finish<\/p>\n<p>This will create a project with directories using Maven conventions:<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-conventions.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-159\" title=\"maven-conventions\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-conventions.png\" alt=\"\" width=\"226\" height=\"170\" \/><\/a><\/p>\n<p>(Note: If your starting point is a new or existing <strong>Java <\/strong>project in Eclipse, you can right click on the project, then choose Configure &gt; Convert to Maven project)<\/p>\n<p><strong>Step 5 &#8211; Add docx4j to your POM<\/strong><\/p>\n<p>Double Click on pom.xml<\/p>\n<p>Next click on the dependencies tab, then click the &#8220;add dependency&#8221; button, and enter the docx4j coordinates as shown in the image below:<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/m2e-select-dependency.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-156\" title=\"m2e-select-dependency\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/m2e-select-dependency.png\" alt=\"\" width=\"678\" height=\"587\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/m2e-select-dependency.png 678w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/m2e-select-dependency-300x259.png 300w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/a><\/p>\n<p>The result is this pom:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;&gt;\r\n  &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n  &lt;groupId&gt;mygroup&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;myartifact&lt;\/artifactId&gt;\r\n  &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n  &lt;dependencies&gt;\r\n  \t&lt;dependency&gt;\r\n  \t\t&lt;groupId&gt;org.docx4j&lt;\/groupId&gt;\r\n  \t\t&lt;artifactId&gt;docx4j&lt;\/artifactId&gt;\r\n  \t\t&lt;version&gt;2.7.1&lt;\/version&gt;\r\n  \t&lt;\/dependency&gt;\r\n  &lt;\/dependencies&gt;\r\n&lt;\/project&gt;\r\n\r\n<\/pre>\n<p>Ctrl-S to save it.<\/p>\n<p>m2eclipse may take some time to download the dependencies.<\/p>\n<p>When it has finished, you should be able to see them:<\/p>\n<p><a href=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-dependencies.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-160\" title=\"maven-dependencies\" src=\"http:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-dependencies.png\" alt=\"\" width=\"472\" height=\"473\" srcset=\"https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-dependencies.png 472w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-dependencies-150x150.png 150w, https:\/\/www.docx4java.org\/blog\/wp-content\/uploads\/2011\/10\/maven-dependencies-300x300.png 300w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><\/a><\/p>\n<p><strong>Step 6 &#8211; Create HelloMavenCentral.java<\/strong><\/p>\n<p>If you made a Maven project as per step 4 above, you should already have  src\/main\/java on your build path.<\/p>\n<p>If not, create the folder and add it.<\/p>\n<p>Now add a new class:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.docx4j.openpackaging.packages.WordprocessingMLPackage;\r\n\r\npublic class HelloMavenCentral {\r\n\r\n\tpublic static void main(String&#x5B;] args) throws Exception {\r\n\t\t\r\n\t\tWordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();\r\n\t\t\r\n\t\twordMLPackage.getMainDocumentPart()\r\n\t\t\t.addStyledParagraphOfText(&quot;Title&quot;, &quot;Hello Maven Central&quot;);\r\n\r\n\t\twordMLPackage.getMainDocumentPart().addParagraphOfText(&quot;from docx4j!&quot;);\r\n\t\t\t\t\r\n\t\t\/\/ Now save it \r\n\t\twordMLPackage.save(new java.io.File(System.getProperty(&quot;user.dir&quot;) + &quot;\/helloMavenCentral.docx&quot;) );\r\n\t\t\r\n\t}\t\r\n}\r\n\r\n<\/pre>\n<p><strong>Step 7 &#8211; Click Run<\/strong><\/p>\n<p>When you click run, all being well, a new docx called helloMavenCentral.docx will be saved.<\/p>\n<p>You can open it in Word (or anything else which can read docx), or unzip it to inspect its contents.<\/p>\n<p><strong>Step 8 &#8211; Adding docx4j.properties<\/strong><\/p>\n<p>One final thing. If you plan on creating documents from scratch using docx4j, it is useful to set paper size etc, via docx4j.properties.  Put something like the following on your path:<\/p>\n<pre># Page size: use a value from org.docx4j.model.structure.PageSizePaper enum\r\n# eg A4, LETTER\r\ndocx4j.PageSize=LETTER\r\n# Page size: use a value from org.docx4j.model.structure.MarginsWellKnown enum\r\ndocx4j.PageMargins=NORMAL\r\ndocx4j.PageOrientationLandscape=false\r\n\r\n# Page size: use a value from org.pptx4j.model.SlideSizesWellKnown enum\r\n# eg A4, LETTER\r\npptx4j.PageSize=LETTER\r\npptx4j.PageOrientationLandscape=false\r\n\r\n# These will be injected into docProps\/app.xml\r\n# if App.Write=true\r\ndocx4j.App.write=true\r\ndocx4j.Application=docx4j\r\ndocx4j.AppVersion=2.7.1\r\n# of the form XX.YYYY where X and Y represent numerical values\r\n\r\n# These will be injected into docProps\/core.xml\r\ndocx4j.dc.write=true\r\ndocx4j.dc.creator.value=docx4j\r\ndocx4j.dc.lastModifiedBy.value=docx4j\r\n\r\n#\r\n#docx4j.McPreprocessor=true\r\n\r\n# If you haven't configured log4j yourself\r\n# docx4j will autoconfigure it.  Set this to true to disable that\r\ndocx4j.Log4j.Configurator.disabled=false<\/pre>\n<p>And that&#8217;s it.  For more information on docx4j, see our <a href=\"\/svn\/docx4j\/trunk\/docx4j\/docs\/Docx4j_GettingStarted.html\">Getting Started<\/a> document.<\/p>\n<p>Please click the +1 button if you found this article helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With version 2.7.1, docx4j &#8211; a library for manipulating Word docx, Powerpoint pptx, and Excel xlsx xml files in Java &#8211; and all its dependencies, are available from Maven Central. This makes it really easy to get going with docx4j.\u00a0 With Eclipse and m2eclipse installed, you just add docx4j, and you&#8217;re done.\u00a0 No need to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,71,1],"tags":[81,82,80],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-docx4j","category-microsoft-word","category-uncategorized","tag-eclipse","tag-m2e","tag-maven-central"],"_links":{"self":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":23,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}